@@ -353,29 +353,48 @@ define(function (require, exports, module) {
353353 /**
354354 * Start the 10-minute interval timer for monitoring entitlements
355355 */
356- function startEntitlementsMonitor ( ) {
356+ function startEffectiveEntitlementsMonitor ( ) {
357+ // Reconcile effective entitlements from server. So the effective entitlements api injects trial
358+ // entitlements data. but only the server fetch will trigger the entitlements change event.
359+ // so in here, we observe the effective entitlements, and if the effective entitlements are changed,
360+ // since the last triggered state, we trigger a change event. This only concerens with the effective
361+ // entitlement changes. This will not logout the user if user logged out from the server admin panel,
362+ // but his entitlements will be cleared by this call anyways.
363+
364+ // At app start we refresh entitlements, then only one each user action like user clicks on profile icon,
365+ // or if some user hits some backend api, we will refresh entitlements. But here, we periodically refresh
366+ // entitlements from the server every 10 minutes, but only trigger entitlement change events only if some
367+ // effective entitlement(Eg. trial) data changed or any validity expired.
368+ if ( Phoenix . isTestWindow ) {
369+ return ;
370+ }
371+ setTimeout ( async function ( ) {
372+ lastRecordedState = await getEffectiveEntitlements ( false ) ;
373+ } , 30000 ) ;
357374 setInterval ( async ( ) => {
358375 try {
359- const current = await getEffectiveEntitlements ( false ) ; // Get effective entitlements
376+ // Get fresh effective entitlements
377+ const freshEntitlements = await getEffectiveEntitlements ( true ) ;
360378
361379 // Check if we need to refresh
362- const expiredPlanName = KernalModeTrust . LoginUtils . validTillExpired ( current , lastRecordedState ) ;
363- const hasChanged = KernalModeTrust . LoginUtils . haveEntitlementsChanged ( current , lastRecordedState ) ;
380+ const expiredPlanName = KernalModeTrust . LoginUtils
381+ . validTillExpired ( freshEntitlements , lastRecordedState ) ;
382+ const hasChanged = KernalModeTrust . LoginUtils
383+ . haveEntitlementsChanged ( freshEntitlements , lastRecordedState ) ;
364384
365385 if ( expiredPlanName || hasChanged ) {
366386 console . log ( `Entitlements monitor detected changes, Expired: ${ expiredPlanName } ,` +
367387 `changed: ${ hasChanged } refreshing...` ) ;
368388 Metrics . countEvent ( Metrics . EVENT_TYPE . PRO , "entRefresh" ,
369389 expiredPlanName ? "exp_" + expiredPlanName : "changed" ) ;
370- await getEffectiveEntitlements ( true ) ; // Force refresh
371390 // if not logged in, the getEffectiveEntitlements will not trigger change even if some trial
372391 // entitlements changed. so we trigger a change anyway here. The debounce will take care of
373392 // multi fire and we are ok with multi fire 1 second apart.
374393 _debounceEntitlementsChanged ( ) ;
375394 }
376395
377396 // Update last recorded state
378- lastRecordedState = current ;
397+ lastRecordedState = freshEntitlements ;
379398 } catch ( error ) {
380399 console . error ( 'Entitlements monitor error:' , error ) ;
381400 }
@@ -598,7 +617,7 @@ define(function (require, exports, module) {
598617 }
599618
600619 // Start the entitlements monitor timer
601- startEntitlementsMonitor ( ) ;
620+ startEffectiveEntitlementsMonitor ( ) ;
602621
603622 exports . init = init ;
604623 // no public exports to prevent extension tampering
0 commit comments