@@ -80,7 +80,8 @@ angular.module('mm.core')
8080 var enabledHandlers = { } ,
8181 self = { } ,
8282 deferreds = { } ,
83- statusCache = { } ; // To speed up the getModulesStatus function.
83+ statusCache = { } , // To speed up the getModulesStatus function.
84+ lastUpdateHandlersStart ;
8485
8586 $log = $log . getInstance ( '$mmCoursePrefetchDelegate' ) ;
8687
@@ -335,6 +336,23 @@ angular.module('mm.core')
335336 return deferreds [ $mmSite . getId ( ) ] && deferreds [ $mmSite . getId ( ) ] [ id ] ;
336337 } ;
337338
339+ /**
340+ * Check if a time belongs to the last update handlers call.
341+ * This is to handle the cases where updatePrefetchHandlers don't finish in the same order as they're called.
342+ *
343+ * @module mm.core
344+ * @ngdoc method
345+ * @name $mmCoursePrefetchDelegate#isLastUpdateCall
346+ * @param {Number } time Time to check.
347+ * @return {Boolean } True if equal, false otherwise.
348+ */
349+ self . isLastUpdateCall = function ( time ) {
350+ if ( ! lastUpdateHandlersStart ) {
351+ return true ;
352+ }
353+ return time == lastUpdateHandlersStart ;
354+ } ;
355+
338356 /**
339357 * Check if a module is downloadable.
340358 *
@@ -436,11 +454,13 @@ angular.module('mm.core')
436454 * @name $mmCoursePrefetchDelegate#updatePrefetchHandler
437455 * @param {String } handles The module this handler handles, e.g. forum, label.
438456 * @param {Object } handlerInfo The handler details.
457+ * @param {Number } time Time this update process started.
439458 * @return {Promise } Resolved when enabled, rejected when not.
440459 * @protected
441460 */
442- self . updatePrefetchHandler = function ( handles , handlerInfo ) {
443- var promise ;
461+ self . updatePrefetchHandler = function ( handles , handlerInfo , time ) {
462+ var promise ,
463+ siteId = $mmSite . getId ( ) ;
444464
445465 if ( typeof handlerInfo . instance === 'undefined' ) {
446466 handlerInfo . instance = $mmUtil . resolveObject ( handlerInfo . handler , true ) ;
@@ -453,14 +473,18 @@ angular.module('mm.core')
453473 }
454474
455475 // Checks if the prefetch is enabled.
456- return promise . then ( function ( enabled ) {
457- if ( enabled ) {
458- enabledHandlers [ handles ] = handlerInfo . instance ;
459- } else {
460- return $q . reject ( ) ;
476+ return promise . catch ( function ( ) {
477+ return false ;
478+ } ) . then ( function ( enabled ) {
479+ // Verify that this call is the last one that was started.
480+ // Check that site hasn't changed since the check started.
481+ if ( self . isLastUpdateCall ( time ) && $mmSite . isLoggedIn ( ) && $mmSite . getId ( ) === siteId ) {
482+ if ( enabled ) {
483+ enabledHandlers [ handles ] = handlerInfo . instance ;
484+ } else {
485+ delete enabledHandlers [ handles ] ;
486+ }
461487 }
462- } ) . catch ( function ( ) {
463- delete enabledHandlers [ handles ] ;
464488 } ) ;
465489 } ;
466490
@@ -474,13 +498,16 @@ angular.module('mm.core')
474498 * @protected
475499 */
476500 self . updatePrefetchHandlers = function ( ) {
477- var promises = [ ] ;
501+ var promises = [ ] ,
502+ now = new Date ( ) . getTime ( ) ;
478503
479504 $log . debug ( 'Updating prefetch handlers for current site.' ) ;
480505
506+ lastUpdateHandlersStart = now ;
507+
481508 // Loop over all the prefetch handlers.
482509 angular . forEach ( prefetchHandlers , function ( handlerInfo , handles ) {
483- promises . push ( self . updatePrefetchHandler ( handles , handlerInfo ) ) ;
510+ promises . push ( self . updatePrefetchHandler ( handles , handlerInfo , now ) ) ;
484511 } ) ;
485512
486513 return $q . all ( promises ) . then ( function ( ) {
@@ -527,9 +554,10 @@ angular.module('mm.core')
527554} )
528555
529556. run ( function ( $mmEvents , mmCoreEventLogin , mmCoreEventSiteUpdated , mmCoreEventLogout , $mmCoursePrefetchDelegate , $mmSite ,
530- mmCoreEventPackageStatusChanged ) {
557+ mmCoreEventPackageStatusChanged , mmCoreEventRemoteAddonsLoaded ) {
531558 $mmEvents . on ( mmCoreEventLogin , $mmCoursePrefetchDelegate . updatePrefetchHandlers ) ;
532559 $mmEvents . on ( mmCoreEventSiteUpdated , $mmCoursePrefetchDelegate . updatePrefetchHandlers ) ;
560+ $mmEvents . on ( mmCoreEventRemoteAddonsLoaded , $mmCoursePrefetchDelegate . updatePrefetchHandlers ) ;
533561 $mmEvents . on ( mmCoreEventLogout , $mmCoursePrefetchDelegate . clearStatusCache ) ;
534562 $mmEvents . on ( mmCoreEventPackageStatusChanged , function ( data ) {
535563 if ( data . siteid === $mmSite . getId ( ) ) {
0 commit comments