From 21b43d56df399b0600853ce7a739f7c1ac86f7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 8 Mar 2024 14:38:28 +0100 Subject: [PATCH] MOBILE-4538 delegates: Add handlers promise enabled info and remove code --- .../services/messageoutput-delegate.ts | 2 +- .../mod/assign/services/feedback-delegate.ts | 2 +- .../assign/services/submission-delegate.ts | 2 +- .../mod/data/services/data-fields-delegate.ts | 2 +- .../quiz/services/access-rules-delegate.ts | 2 +- .../services/assessment-strategy-delegate.ts | 2 +- src/core/classes/delegate-sorted.ts | 2 +- src/core/classes/delegate.ts | 63 ++++++++++--------- .../features/block/services/block-delegate.ts | 2 +- .../course/services/format-delegate.ts | 2 +- .../course/services/module-delegate.ts | 2 +- .../services/module-prefetch-delegate.ts | 2 +- .../features/enrol/services/enrol-delegate.ts | 2 +- .../services/fileuploader-delegate.ts | 2 +- .../filter/services/filter-delegate.ts | 2 +- .../question/services/behaviour-delegate.ts | 2 +- .../question/services/question-delegate.ts | 2 +- .../tag/services/tag-area-delegate.ts | 2 +- .../features/user/services/user-delegate.ts | 2 +- .../services/user-profile-field-delegate.ts | 2 +- src/core/services/analytics.ts | 2 +- src/core/services/plugin-file-delegate.ts | 2 +- 22 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/addons/messageoutput/services/messageoutput-delegate.ts b/src/addons/messageoutput/services/messageoutput-delegate.ts index ba99f0647..0e6837158 100644 --- a/src/addons/messageoutput/services/messageoutput-delegate.ts +++ b/src/addons/messageoutput/services/messageoutput-delegate.ts @@ -75,7 +75,7 @@ export class AddonMessageOutputDelegateService extends CoreDelegate this.clearSortedHandlers()); CoreEvents.on(CoreEvents.SITE_POLICY_AGREED, (data) => { diff --git a/src/core/classes/delegate.ts b/src/core/classes/delegate.ts index ba2b73b0f..c98cd6f3a 100644 --- a/src/core/classes/delegate.ts +++ b/src/core/classes/delegate.ts @@ -72,43 +72,42 @@ export class CoreDelegate { /** * Promise to wait for handlers to be initialized. + * + * @returns Promise resolved when handlers are enabled. */ - protected handlersInitPromise: Promise; + protected handlersInitPromise: Promise; /** * Function to resolve the handlers init promise. */ - protected handlersInitResolve!: () => void; + protected handlersInitResolve!: (enabled: boolean) => void; /** * Constructor of the Delegate. * * @param delegateName Delegate name used for logging purposes. - * @param listenSiteEvents Whether to update the handler when a site event occurs (login, site updated, ...). */ - constructor(delegateName: string, listenSiteEvents: boolean = true) { + constructor(delegateName: string) { this.logger = CoreLogger.getInstance(delegateName); this.handlersInitPromise = new Promise((resolve): void => { this.handlersInitResolve = resolve; }); - if (listenSiteEvents) { - // Update handlers on this cases. - CoreEvents.on(CoreEvents.LOGIN, () => this.updateHandlers()); - CoreEvents.on(CoreEvents.SITE_UPDATED, () => this.updateHandlers()); - CoreEvents.on(CoreEvents.SITE_PLUGINS_LOADED, () => this.updateHandlers()); - CoreEvents.on(CoreEvents.SITE_POLICY_AGREED, (data) => { - if (data.siteId === CoreSites.getCurrentSiteId()) { - this.updateHandlers(); - } - }); - CoreEvents.on(CoreEvents.COMPLETE_REQUIRED_PROFILE_DATA_FINISHED, (data) => { - if (data.siteId === CoreSites.getCurrentSiteId()) { - this.updateHandlers(); - } - }); - } + // Update handlers on this cases. + CoreEvents.on(CoreEvents.LOGIN, () => this.updateHandlers()); + CoreEvents.on(CoreEvents.SITE_UPDATED, () => this.updateHandlers()); + CoreEvents.on(CoreEvents.SITE_PLUGINS_LOADED, () => this.updateHandlers()); + CoreEvents.on(CoreEvents.SITE_POLICY_AGREED, (data) => { + if (data.siteId === CoreSites.getCurrentSiteId()) { + this.updateHandlers(); + } + }); + CoreEvents.on(CoreEvents.COMPLETE_REQUIRED_PROFILE_DATA_FINISHED, (data) => { + if (data.siteId === CoreSites.getCurrentSiteId()) { + this.updateHandlers(); + } + }); } /** @@ -286,16 +285,18 @@ export class CoreDelegate { } // Checks if the handler is enabled. - this.updatePromises[siteId][handler.name] = promise.then((enabled: boolean) => { + this.updatePromises[siteId][handler.name] = promise.then((enabled) => { // Check that site hasn't changed since the check started. - if (CoreSites.getCurrentSiteId() === siteId) { - const key = handler[this.handlerNameProperty] || handler.name; + if (CoreSites.getCurrentSiteId() !== siteId) { + return; + } - if (enabled) { - this.enabledHandlers[key] = handler; - } else { - delete this.enabledHandlers[key]; - } + const key = handler[this.handlerNameProperty] || handler.name; + + if (enabled) { + this.enabledHandlers[key] = handler; + } else { + delete this.enabledHandlers[key]; } return; @@ -327,6 +328,10 @@ export class CoreDelegate { const enabled = await this.isEnabled(); if (!enabled) { + this.logger.debug('Delegate not enabled.'); + + this.handlersInitResolve(false); + return; } @@ -351,7 +356,7 @@ export class CoreDelegate { // Verify that this call is the last one that was started. if (this.isLastUpdateCall(now)) { this.handlersInitialized = true; - this.handlersInitResolve(); + this.handlersInitResolve(true); this.updateData(); } diff --git a/src/core/features/block/services/block-delegate.ts b/src/core/features/block/services/block-delegate.ts index 6f5e46f61..ed4de1719 100644 --- a/src/core/features/block/services/block-delegate.ts +++ b/src/core/features/block/services/block-delegate.ts @@ -104,7 +104,7 @@ export class CoreBlockDelegateService extends CoreDelegate { constructor( protected defaultHandler: CoreBlockDefaultHandler, ) { - super('CoreBlockDelegate', true); + super('CoreBlockDelegate'); this.blocksUpdateObservable = new Subject(); } diff --git a/src/core/features/course/services/format-delegate.ts b/src/core/features/course/services/format-delegate.ts index d19f9ccaf..bcfaabe78 100644 --- a/src/core/features/course/services/format-delegate.ts +++ b/src/core/features/course/services/format-delegate.ts @@ -166,7 +166,7 @@ export class CoreCourseFormatDelegateService extends CoreDelegate> = {}; constructor() { - super('CoreCourseModulePrefetchDelegate', true); + super('CoreCourseModulePrefetchDelegate'); } /** diff --git a/src/core/features/enrol/services/enrol-delegate.ts b/src/core/features/enrol/services/enrol-delegate.ts index 3ef216fda..81bf6ec62 100644 --- a/src/core/features/enrol/services/enrol-delegate.ts +++ b/src/core/features/enrol/services/enrol-delegate.ts @@ -129,7 +129,7 @@ export class CoreEnrolDelegateService extends CoreDelegate { protected featurePrefix = 'CoreEnrolDelegate_'; constructor() { - super('CoreEnrolDelegate', true); + super('CoreEnrolDelegate'); } /** diff --git a/src/core/features/fileuploader/services/fileuploader-delegate.ts b/src/core/features/fileuploader/services/fileuploader-delegate.ts index 04683f7d6..d0bcfe12c 100644 --- a/src/core/features/fileuploader/services/fileuploader-delegate.ts +++ b/src/core/features/fileuploader/services/fileuploader-delegate.ts @@ -143,7 +143,7 @@ export interface CoreFileUploaderHandlerDataToReturn extends CoreFileUploaderHan export class CoreFileUploaderDelegateService extends CoreDelegate { constructor() { - super('CoreFileUploaderDelegate', true); + super('CoreFileUploaderDelegate'); CoreEvents.on(CoreEvents.LOGOUT, () => this.clearSiteHandlers()); } diff --git a/src/core/features/filter/services/filter-delegate.ts b/src/core/features/filter/services/filter-delegate.ts index 07031aae7..9179bacc2 100644 --- a/src/core/features/filter/services/filter-delegate.ts +++ b/src/core/features/filter/services/filter-delegate.ts @@ -84,7 +84,7 @@ export class CoreFilterDelegateService extends CoreDelegate { protected handlerNameProperty = 'filterName'; constructor(protected defaultHandler: CoreFilterDefaultHandler) { - super('CoreFilterDelegate', true); + super('CoreFilterDelegate'); } /** diff --git a/src/core/features/question/services/behaviour-delegate.ts b/src/core/features/question/services/behaviour-delegate.ts index cbe9856fa..195e3ffc1 100644 --- a/src/core/features/question/services/behaviour-delegate.ts +++ b/src/core/features/question/services/behaviour-delegate.ts @@ -68,7 +68,7 @@ export class CoreQuestionBehaviourDelegateService extends CoreDelegate protected handlerNameProperty = 'type'; constructor() { - super('CoreTagAreaDelegate', true); + super('CoreTagAreaDelegate'); } /** diff --git a/src/core/features/user/services/user-delegate.ts b/src/core/features/user/services/user-delegate.ts index 937cb5bf1..60bd7f31c 100644 --- a/src/core/features/user/services/user-delegate.ts +++ b/src/core/features/user/services/user-delegate.ts @@ -229,7 +229,7 @@ export class CoreUserDelegateService extends CoreDelegate> = {}; constructor() { - super('CoreUserDelegate', true); + super('CoreUserDelegate'); CoreEvents.on(USER_DELEGATE_UPDATE_HANDLER_EVENT, (data) => { const handlersData = this.getHandlersData(data.userId, data.context, data.contextId); diff --git a/src/core/features/user/services/user-profile-field-delegate.ts b/src/core/features/user/services/user-profile-field-delegate.ts index 14d7492ed..cc29fe212 100644 --- a/src/core/features/user/services/user-profile-field-delegate.ts +++ b/src/core/features/user/services/user-profile-field-delegate.ts @@ -80,7 +80,7 @@ export class CoreUserProfileFieldDelegateService extends CoreDelegate { constructor() { - super('CoreAnalyticsService', true); + super('CoreAnalyticsService'); CoreEvents.on(CoreConfigProvider.ENVIRONMENT_UPDATED, () => this.updateHandlers()); CoreEvents.on(CoreEvents.LOGOUT, () => this.clearSiteHandlers()); diff --git a/src/core/services/plugin-file-delegate.ts b/src/core/services/plugin-file-delegate.ts index 69a640e1a..456476f74 100644 --- a/src/core/services/plugin-file-delegate.ts +++ b/src/core/services/plugin-file-delegate.ts @@ -30,7 +30,7 @@ import { CoreFileHelper } from './file-helper'; export class CorePluginFileDelegateService extends CoreDelegate { constructor() { - super('CorePluginFileDelegate', true); + super('CorePluginFileDelegate'); } /**