From 69cf1ead4cd6d853d1415ad87513687a0039ebfe Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 7 Mar 2024 12:02:10 +0100 Subject: [PATCH 1/2] MOBILE-4529 survey: Decouple from initial bundle --- .../mod/survey/components/index/index.ts | 4 +- src/addons/mod/survey/constants.ts | 26 ++++ .../mod/survey/services/handlers/module.ts | 8 +- .../survey/services/handlers/prefetch-lazy.ts | 109 +++++++++++++++ .../mod/survey/services/handlers/prefetch.ts | 132 +++++------------- .../services/handlers/sync-cron-lazy.ts | 44 ++++++ .../mod/survey/services/handlers/sync-cron.ts | 50 +++---- src/addons/mod/survey/services/survey-sync.ts | 4 +- src/addons/mod/survey/services/survey.ts | 3 +- src/addons/mod/survey/survey.module.ts | 27 ++-- src/core/features/compile/services/compile.ts | 4 +- .../features/course/classes/activity-sync.ts | 5 +- 12 files changed, 276 insertions(+), 140 deletions(-) create mode 100644 src/addons/mod/survey/constants.ts create mode 100644 src/addons/mod/survey/services/handlers/prefetch-lazy.ts create mode 100644 src/addons/mod/survey/services/handlers/sync-cron-lazy.ts diff --git a/src/addons/mod/survey/components/index/index.ts b/src/addons/mod/survey/components/index/index.ts index 287830ed8..e7570b19f 100644 --- a/src/addons/mod/survey/components/index/index.ts +++ b/src/addons/mod/survey/components/index/index.ts @@ -23,7 +23,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreTextUtils } from '@services/utils/text'; import { Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; -import { AddonModSurveyPrefetchHandler } from '../../services/handlers/prefetch'; +import { getPrefetchHandlerInstance } from '../../services/handlers/prefetch'; import { AddonModSurveyProvider, AddonModSurveySurvey, @@ -215,7 +215,7 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo // The survey is downloaded, update the data. try { const prefetched = await AddonModSurveySync.prefetchAfterUpdate( - AddonModSurveyPrefetchHandler.instance, + getPrefetchHandlerInstance(), this.module, this.courseId, ); diff --git a/src/addons/mod/survey/constants.ts b/src/addons/mod/survey/constants.ts new file mode 100644 index 000000000..bcbc93f72 --- /dev/null +++ b/src/addons/mod/survey/constants.ts @@ -0,0 +1,26 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +export const ADDON_MOD_SURVEY_COMPONENT = 'mmaModSurvey'; + +// Routing. +export const ADDON_MOD_SURVEY_PAGE_NAME = 'mod_survey'; + +// Handlers. +export const ADDON_MOD_SURVEY_PREFETCH_NAME = 'AddonModSurvey'; +export const ADDON_MOD_SURVEY_PREFETCH_MODNAME = 'survey'; +export const ADDON_MOD_SURVEY_PREFETCH_COMPONENT = ADDON_MOD_SURVEY_COMPONENT; +export const ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES = /^configuration$|^.*files$|^answers$/; + +export const ADDON_MOD_SURVEY_SYNC_CRON_NAME = 'AddonModSurveySyncCronHandler'; diff --git a/src/addons/mod/survey/services/handlers/module.ts b/src/addons/mod/survey/services/handlers/module.ts index 1faa1a25a..a81fc018c 100644 --- a/src/addons/mod/survey/services/handlers/module.ts +++ b/src/addons/mod/survey/services/handlers/module.ts @@ -13,11 +13,11 @@ // limitations under the License. import { CoreConstants, ModPurpose } from '@/core/constants'; +import { ADDON_MOD_SURVEY_PAGE_NAME } from '@addons/mod/survey/constants'; import { Injectable, Type } from '@angular/core'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; import { CoreCourseModuleHandler } from '@features/course/services/module-delegate'; import { makeSingleton } from '@singletons'; -import { AddonModSurveyIndexComponent } from '../../components/index'; /** * Handler to support survey modules. @@ -25,11 +25,9 @@ import { AddonModSurveyIndexComponent } from '../../components/index'; @Injectable( { providedIn: 'root' }) export class AddonModSurveyModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler { - static readonly PAGE_NAME = 'mod_survey'; - name = 'AddonModSurvey'; modName = 'survey'; - protected pageName = AddonModSurveyModuleHandlerService.PAGE_NAME; + protected pageName = ADDON_MOD_SURVEY_PAGE_NAME; supportedFeatures = { [CoreConstants.FEATURE_GROUPS]: true, @@ -48,6 +46,8 @@ export class AddonModSurveyModuleHandlerService extends CoreModuleHandlerBase im * @inheritdoc */ async getMainComponent(): Promise> { + const { AddonModSurveyIndexComponent } = await import('@addons/mod/survey/components/index'); + return AddonModSurveyIndexComponent; } diff --git a/src/addons/mod/survey/services/handlers/prefetch-lazy.ts b/src/addons/mod/survey/services/handlers/prefetch-lazy.ts new file mode 100644 index 000000000..f8b361afa --- /dev/null +++ b/src/addons/mod/survey/services/handlers/prefetch-lazy.ts @@ -0,0 +1,109 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; +import { CoreCourseAnyModuleData } from '@features/course/services/course'; +import { CoreFilepool } from '@services/filepool'; +import { CoreSitesReadingStrategy } from '@services/sites'; +import { CoreUtils } from '@services/utils/utils'; +import { CoreWSFile } from '@services/ws'; +import { makeSingleton } from '@singletons'; +import { AddonModSurvey, AddonModSurveyProvider } from '../survey'; +import { AddonModSurveySync, AddonModSurveySyncResult } from '../survey-sync'; +import { AddonModSurveyPrefetchHandlerService } from '@addons/mod/survey/services/handlers/prefetch'; + +/** + * Handler to prefetch surveys. + */ +@Injectable( { providedIn: 'root' }) +export class AddonModSurveyPrefetchHandlerLazyService extends AddonModSurveyPrefetchHandlerService { + + /** + * @inheritdoc + */ + async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { + const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id)); + + return this.getIntroFilesFromInstance(module, survey); + } + + /** + * @inheritdoc + */ + async invalidateContent(moduleId: number, courseId: number): Promise { + return AddonModSurvey.invalidateContent(moduleId, courseId); + } + + /** + * @inheritdoc + */ + async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise { + await AddonModSurvey.invalidateSurveyData(courseId); + } + + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return true; + } + + /** + * @inheritdoc + */ + prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise { + return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId)); + } + + /** + * Prefetch a survey. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @param siteId SiteId or current site. + * @returns Promise resolved when done. + */ + protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise { + const survey = await AddonModSurvey.getSurvey(courseId, module.id, { + readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, + siteId, + }); + + const promises: Promise[] = []; + const files = this.getIntroFilesFromInstance(module, survey); + + // Prefetch files. + promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id)); + + // If survey isn't answered, prefetch the questions. + if (!survey.surveydone) { + promises.push(AddonModSurvey.getQuestions(survey.id, { + cmId: module.id, + readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, + siteId, + })); + } + + await Promise.all(promises); + } + + /** + * @inheritdoc + */ + sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { + return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId); + } + +} +export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerLazyService); diff --git a/src/addons/mod/survey/services/handlers/prefetch.ts b/src/addons/mod/survey/services/handlers/prefetch.ts index fb1a0239d..931964d7c 100644 --- a/src/addons/mod/survey/services/handlers/prefetch.ts +++ b/src/addons/mod/survey/services/handlers/prefetch.ts @@ -12,103 +12,47 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { AsyncInstance, asyncInstance } from '@/core/utils/async-instance'; +import { + ADDON_MOD_SURVEY_PREFETCH_COMPONENT, + ADDON_MOD_SURVEY_PREFETCH_MODNAME, + ADDON_MOD_SURVEY_PREFETCH_NAME, + ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES, +} from '@addons/mod/survey/constants'; import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler'; -import { CoreCourseAnyModuleData } from '@features/course/services/course'; -import { CoreFilepool } from '@services/filepool'; -import { CoreSitesReadingStrategy } from '@services/sites'; -import { CoreUtils } from '@services/utils/utils'; -import { CoreWSFile } from '@services/ws'; -import { makeSingleton } from '@singletons'; -import { AddonModSurvey, AddonModSurveyProvider } from '../survey'; -import { AddonModSurveySync, AddonModSurveySyncResult } from '../survey-sync'; +import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate'; +import type { AddonModSurveyPrefetchHandlerLazyService } from './prefetch-lazy'; + +let prefetchHandlerInstance: AsyncInstance< + AddonModSurveyPrefetchHandlerLazyService, + AddonModSurveyPrefetchHandlerService +> | null = null; -/** - * Handler to prefetch surveys. - */ -@Injectable( { providedIn: 'root' }) export class AddonModSurveyPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase { - name = 'AddonModSurvey'; - modName = 'survey'; - component = AddonModSurveyProvider.COMPONENT; - updatesNames = /^configuration$|^.*files$|^answers$/; - - /** - * @inheritdoc - */ - async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { - const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id)); - - return this.getIntroFilesFromInstance(module, survey); - } - - /** - * @inheritdoc - */ - async invalidateContent(moduleId: number, courseId: number): Promise { - return AddonModSurvey.invalidateContent(moduleId, courseId); - } - - /** - * @inheritdoc - */ - async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise { - await AddonModSurvey.invalidateSurveyData(courseId); - } - - /** - * @inheritdoc - */ - async isEnabled(): Promise { - return true; - } - - /** - * @inheritdoc - */ - prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise { - return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId)); - } - - /** - * Prefetch a survey. - * - * @param module Module. - * @param courseId Course ID the module belongs to. - * @param siteId SiteId or current site. - * @returns Promise resolved when done. - */ - protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise { - const survey = await AddonModSurvey.getSurvey(courseId, module.id, { - readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, - siteId, - }); - - const promises: Promise[] = []; - const files = this.getIntroFilesFromInstance(module, survey); - - // Prefetch files. - promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id)); - - // If survey isn't answered, prefetch the questions. - if (!survey.surveydone) { - promises.push(AddonModSurvey.getQuestions(survey.id, { - cmId: module.id, - readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, - siteId, - })); - } - - await Promise.all(promises); - } - - /** - * @inheritdoc - */ - sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { - return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId); - } + name = ADDON_MOD_SURVEY_PREFETCH_NAME; + modName = ADDON_MOD_SURVEY_PREFETCH_MODNAME; + component = ADDON_MOD_SURVEY_PREFETCH_COMPONENT; + updatesNames = ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES; } -export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerService); + +/** + * Get prefetch handler instance. + * + * @returns Prefetch handler. + */ +export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler { + if (!prefetchHandlerInstance) { + prefetchHandlerInstance = asyncInstance(async () => { + const { AddonModSurveyPrefetchHandler } = await import('./prefetch-lazy'); + + return AddonModSurveyPrefetchHandler.instance; + }); + + prefetchHandlerInstance.setEagerInstance(new AddonModSurveyPrefetchHandlerService()); + prefetchHandlerInstance.setLazyInstanceMethods(['sync']); + } + + return prefetchHandlerInstance; +} diff --git a/src/addons/mod/survey/services/handlers/sync-cron-lazy.ts b/src/addons/mod/survey/services/handlers/sync-cron-lazy.ts new file mode 100644 index 000000000..72f502f6a --- /dev/null +++ b/src/addons/mod/survey/services/handlers/sync-cron-lazy.ts @@ -0,0 +1,44 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; +import { CoreCronHandler } from '@services/cron'; +import { makeSingleton } from '@singletons'; +import { AddonModSurveySync } from '../survey-sync'; +import { AddonModSurveySyncCronHandlerService } from '@addons/mod/survey/services/handlers/sync-cron'; + +/** + * Synchronization cron handler. + */ +@Injectable( { providedIn: 'root' }) +export class AddonModSurveySyncCronHandlerLazyService + extends AddonModSurveySyncCronHandlerService + implements CoreCronHandler { + + /** + * @inheritdoc + */ + async execute(siteId?: string, force?: boolean): Promise { + await AddonModSurveySync.syncAllSurveys(siteId, force); + } + + /** + * @inheritdoc + */ + getInterval(): number { + return AddonModSurveySync.syncInterval; + } + +} +export const AddonModSurveySyncCronHandler = makeSingleton(AddonModSurveySyncCronHandlerLazyService); diff --git a/src/addons/mod/survey/services/handlers/sync-cron.ts b/src/addons/mod/survey/services/handlers/sync-cron.ts index bad10bf57..fbd1f1881 100644 --- a/src/addons/mod/survey/services/handlers/sync-cron.ts +++ b/src/addons/mod/survey/services/handlers/sync-cron.ts @@ -12,32 +12,34 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_SURVEY_SYNC_CRON_NAME } from '@addons/mod/survey/constants'; import { CoreCronHandler } from '@services/cron'; -import { makeSingleton } from '@singletons'; -import { AddonModSurveySync } from '../survey-sync'; +import type { AddonModSurveySyncCronHandlerLazyService } from './sync-cron-lazy'; -/** - * Synchronization cron handler. - */ -@Injectable( { providedIn: 'root' }) -export class AddonModSurveySyncCronHandlerService implements CoreCronHandler { +export class AddonModSurveySyncCronHandlerService { - name = 'AddonModSurveySyncCronHandler'; - - /** - * @inheritdoc - */ - async execute(siteId?: string, force?: boolean): Promise { - await AddonModSurveySync.syncAllSurveys(siteId, force); - } - - /** - * @inheritdoc - */ - getInterval(): number { - return AddonModSurveySync.syncInterval; - } + name = ADDON_MOD_SURVEY_SYNC_CRON_NAME; } -export const AddonModSurveySyncCronHandler = makeSingleton(AddonModSurveySyncCronHandlerService); + +/** + * Get cron handler instance. + * + * @returns Cron handler. + */ +export function getCronHandlerInstance(): CoreCronHandler { + const lazyHandler = asyncInstance< + AddonModSurveySyncCronHandlerLazyService, + AddonModSurveySyncCronHandlerService + >(async () => { + const { AddonModSurveySyncCronHandler } = await import('./sync-cron-lazy'); + + return AddonModSurveySyncCronHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModSurveySyncCronHandlerService()); + lazyHandler.setLazyInstanceMethods(['execute', 'getInterval']); + + return lazyHandler; +} diff --git a/src/addons/mod/survey/services/survey-sync.ts b/src/addons/mod/survey/services/survey-sync.ts index 6089e342a..71c32018f 100644 --- a/src/addons/mod/survey/services/survey-sync.ts +++ b/src/addons/mod/survey/services/survey-sync.ts @@ -23,7 +23,7 @@ import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; import { CoreEvents } from '@singletons/events'; -import { AddonModSurveyPrefetchHandler } from './handlers/prefetch'; +import { getPrefetchHandlerInstance } from './handlers/prefetch'; import { AddonModSurvey, AddonModSurveyProvider } from './survey'; import { AddonModSurveyAnswersDBRecordFormatted, AddonModSurveyOffline } from './survey-offline'; @@ -206,7 +206,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid const module = await CoreCourse.getModuleBasicInfoByInstance(surveyId, 'survey', { siteId }); CoreUtils.ignoreErrors( - this.prefetchAfterUpdate(AddonModSurveyPrefetchHandler.instance, module, result.courseId, undefined, siteId), + this.prefetchAfterUpdate(getPrefetchHandlerInstance(), module, result.courseId, undefined, siteId), ); } } diff --git a/src/addons/mod/survey/services/survey.ts b/src/addons/mod/survey/services/survey.ts index cb7d670b3..81824a3e4 100644 --- a/src/addons/mod/survey/services/survey.ts +++ b/src/addons/mod/survey/services/survey.ts @@ -25,6 +25,7 @@ import { CoreStatusWithWarningsWSResponse, CoreWSExternalFile, CoreWSExternalWar import { makeSingleton, Translate } from '@singletons'; import { AddonModSurveyOffline } from './survey-offline'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; +import { ADDON_MOD_SURVEY_COMPONENT } from '@addons/mod/survey/constants'; const ROOT_CACHE_KEY = 'mmaModSurvey:'; @@ -34,7 +35,7 @@ const ROOT_CACHE_KEY = 'mmaModSurvey:'; @Injectable( { providedIn: 'root' }) export class AddonModSurveyProvider { - static readonly COMPONENT = 'mmaModSurvey'; + static readonly COMPONENT = ADDON_MOD_SURVEY_COMPONENT; /** * Get a survey's questions. diff --git a/src/addons/mod/survey/survey.module.ts b/src/addons/mod/survey/survey.module.ts index c502e6ef5..ac12ab352 100644 --- a/src/addons/mod/survey/survey.module.ts +++ b/src/addons/mod/survey/survey.module.ts @@ -20,13 +20,13 @@ import { CoreCourseModulePrefetchDelegate } from '@features/course/services/modu import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module'; import { CoreCronDelegate } from '@services/cron'; import { CORE_SITE_SCHEMAS } from '@services/sites'; -import { AddonModSurveyComponentsModule } from './components/components.module'; import { ADDON_MOD_SURVEY_OFFLINE_SITE_SCHEMA } from './services/database/survey'; import { AddonModSurveyIndexLinkHandler } from './services/handlers/index-link'; import { AddonModSurveyListLinkHandler } from './services/handlers/list-link'; -import { AddonModSurveyModuleHandler, AddonModSurveyModuleHandlerService } from './services/handlers/module'; -import { AddonModSurveyPrefetchHandler } from './services/handlers/prefetch'; -import { AddonModSurveySyncCronHandler } from './services/handlers/sync-cron'; +import { AddonModSurveyModuleHandler } from './services/handlers/module'; +import { getPrefetchHandlerInstance } from './services/handlers/prefetch'; +import { getCronHandlerInstance } from './services/handlers/sync-cron'; +import { ADDON_MOD_SURVEY_PAGE_NAME } from '@addons/mod/survey/constants'; /** * Get mod Survey services. @@ -47,9 +47,20 @@ export async function getModSurveyServices(): Promise[]> { ]; } +/** + * Get survey component modules. + * + * @returns Survey component modules. + */ +export async function getModSurveyComponentModules(): Promise { + const { AddonModSurveyComponentsModule } = await import('@addons/mod/survey/components/components.module'); + + return [AddonModSurveyComponentsModule]; +} + const routes: Routes = [ { - path: AddonModSurveyModuleHandlerService.PAGE_NAME, + path: ADDON_MOD_SURVEY_PAGE_NAME, loadChildren: () => import('./survey-lazy.module').then(m => m.AddonModSurveyLazyModule), }, ]; @@ -57,7 +68,6 @@ const routes: Routes = [ @NgModule({ imports: [ CoreMainMenuTabRoutingModule.forChild(routes), - AddonModSurveyComponentsModule, ], providers: [ { @@ -69,9 +79,10 @@ const routes: Routes = [ provide: APP_INITIALIZER, multi: true, useValue: () => { + CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance()); + CoreCronDelegate.register(getCronHandlerInstance()); + CoreCourseModuleDelegate.registerHandler(AddonModSurveyModuleHandler.instance); - CoreCourseModulePrefetchDelegate.registerHandler(AddonModSurveyPrefetchHandler.instance); - CoreCronDelegate.register(AddonModSurveySyncCronHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModSurveyIndexLinkHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModSurveyListLinkHandler.instance); }, diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index 732cba1ff..4bf951299 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -145,7 +145,7 @@ import { getModPageServices } from '@addons/mod/page/page.module'; import { getModQuizServices } from '@addons/mod/quiz/quiz.module'; import { getModResourceServices } from '@addons/mod/resource/resource.module'; import { getModScormServices } from '@addons/mod/scorm/scorm.module'; -import { getModSurveyServices } from '@addons/mod/survey/survey.module'; +import { getModSurveyComponentModules, getModSurveyServices } from '@addons/mod/survey/survey.module'; import { getModUrlServices } from '@addons/mod/url/url.module'; import { getModWikiServices } from '@addons/mod/wiki/wiki.module'; import { getModWorkshopComponentModules, getModWorkshopServices } from '@addons/mod/workshop/workshop.module'; @@ -184,6 +184,7 @@ export class CoreCompileProvider { protected readonly LAZY_IMPORTS = [ getModWorkshopComponentModules, + getModSurveyComponentModules, ]; constructor(protected injector: Injector) { @@ -383,7 +384,6 @@ export class CoreCompileProvider { getTagServices(), getUsersServices(), getXAPIServices(), - getBadgesServices(), getCalendarServices(), getCompetencyServices(), diff --git a/src/core/features/course/classes/activity-sync.ts b/src/core/features/course/classes/activity-sync.ts index faee1f8e6..0866a244e 100644 --- a/src/core/features/course/classes/activity-sync.ts +++ b/src/core/features/course/classes/activity-sync.ts @@ -14,8 +14,7 @@ import { CoreSyncBaseProvider } from '@classes/base-sync'; import { CoreCourse, CoreCourseAnyModuleData } from '../services/course'; -import { CoreCourseModulePrefetchDelegate } from '../services/module-prefetch-delegate'; -import { CoreCourseModulePrefetchHandlerBase } from './module-prefetch-handler'; +import { CoreCourseModulePrefetchDelegate, CoreCourseModulePrefetchHandler } from '../services/module-prefetch-delegate'; /** * Base class to create activity sync providers. It provides some common functions. @@ -35,7 +34,7 @@ export class CoreCourseActivitySyncBaseProvider extends CoreSyncBasePr * @returns Promise resolved with boolean: true if prefetched, false if no need to prefetch. */ async prefetchAfterUpdate( - prefetchHandler: CoreCourseModulePrefetchHandlerBase, + prefetchHandler: CoreCourseModulePrefetchHandler, module: CoreCourseAnyModuleData, courseId: number, preventDownloadRegex?: RegExp, From b39dc1b90e75381185c2cfffc93999d70da43996 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 7 Mar 2024 15:48:16 +0100 Subject: [PATCH 2/2] MOBILE-4529 addons: Fix lazy instances overrides --- .../mod/survey/services/handlers/prefetch.ts | 9 +++- .../mod/survey/services/handlers/sync-cron.ts | 2 +- .../accumulative/services/handler.ts | 2 +- .../assessment/comments/services/handler.ts | 2 +- .../assessment/numerrors/services/handler.ts | 2 +- .../assessment/rubric/services/handler.ts | 2 +- .../workshop/services/handlers/prefetch.ts | 8 +++- .../workshop/services/handlers/sync-cron.ts | 2 +- src/core/utils/async-instance.ts | 41 ++++++++++++++----- src/core/utils/tests/async-instance.test.ts | 11 ++++- 10 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/addons/mod/survey/services/handlers/prefetch.ts b/src/addons/mod/survey/services/handlers/prefetch.ts index 931964d7c..5ceeeb534 100644 --- a/src/addons/mod/survey/services/handlers/prefetch.ts +++ b/src/addons/mod/survey/services/handlers/prefetch.ts @@ -51,7 +51,14 @@ export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler { }); prefetchHandlerInstance.setEagerInstance(new AddonModSurveyPrefetchHandlerService()); - prefetchHandlerInstance.setLazyInstanceMethods(['sync']); + prefetchHandlerInstance.setLazyMethods(['sync']); + prefetchHandlerInstance.setLazyOverrides([ + 'prefetch', + 'isEnabled', + 'invalidateModule', + 'invalidateContent', + 'getIntroFiles', + ]); } return prefetchHandlerInstance; diff --git a/src/addons/mod/survey/services/handlers/sync-cron.ts b/src/addons/mod/survey/services/handlers/sync-cron.ts index fbd1f1881..c715a8161 100644 --- a/src/addons/mod/survey/services/handlers/sync-cron.ts +++ b/src/addons/mod/survey/services/handlers/sync-cron.ts @@ -39,7 +39,7 @@ export function getCronHandlerInstance(): CoreCronHandler { }); lazyHandler.setEagerInstance(new AddonModSurveySyncCronHandlerService()); - lazyHandler.setLazyInstanceMethods(['execute', 'getInterval']); + lazyHandler.setLazyMethods(['execute', 'getInterval']); return lazyHandler; } diff --git a/src/addons/mod/workshop/assessment/accumulative/services/handler.ts b/src/addons/mod/workshop/assessment/accumulative/services/handler.ts index fd1ab1fba..b6ffd33e8 100644 --- a/src/addons/mod/workshop/assessment/accumulative/services/handler.ts +++ b/src/addons/mod/workshop/assessment/accumulative/services/handler.ts @@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS }); lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyAccumulativeHandlerService()); - lazyHandler.setLazyInstanceMethods([ + lazyHandler.setLazyMethods([ 'getComponent', 'getOriginalValues', 'hasDataChanged', diff --git a/src/addons/mod/workshop/assessment/comments/services/handler.ts b/src/addons/mod/workshop/assessment/comments/services/handler.ts index d30698cd2..6c2ff3a26 100644 --- a/src/addons/mod/workshop/assessment/comments/services/handler.ts +++ b/src/addons/mod/workshop/assessment/comments/services/handler.ts @@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS }); lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyCommentsHandlerService()); - lazyHandler.setLazyInstanceMethods([ + lazyHandler.setLazyMethods([ 'getComponent', 'getOriginalValues', 'hasDataChanged', diff --git a/src/addons/mod/workshop/assessment/numerrors/services/handler.ts b/src/addons/mod/workshop/assessment/numerrors/services/handler.ts index 523cd38b9..da12f8ec9 100644 --- a/src/addons/mod/workshop/assessment/numerrors/services/handler.ts +++ b/src/addons/mod/workshop/assessment/numerrors/services/handler.ts @@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS }); lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyNumErrorsHandlerService()); - lazyHandler.setLazyInstanceMethods([ + lazyHandler.setLazyMethods([ 'getComponent', 'getOriginalValues', 'hasDataChanged', diff --git a/src/addons/mod/workshop/assessment/rubric/services/handler.ts b/src/addons/mod/workshop/assessment/rubric/services/handler.ts index 9b06d28e5..c79371890 100644 --- a/src/addons/mod/workshop/assessment/rubric/services/handler.ts +++ b/src/addons/mod/workshop/assessment/rubric/services/handler.ts @@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS }); lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyRubricHandlerService()); - lazyHandler.setLazyInstanceMethods([ + lazyHandler.setLazyMethods([ 'getComponent', 'getOriginalValues', 'hasDataChanged', diff --git a/src/addons/mod/workshop/services/handlers/prefetch.ts b/src/addons/mod/workshop/services/handlers/prefetch.ts index 5e8fbf422..8ec0f1cd7 100644 --- a/src/addons/mod/workshop/services/handlers/prefetch.ts +++ b/src/addons/mod/workshop/services/handlers/prefetch.ts @@ -48,7 +48,13 @@ export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler { }); lazyHandler.setEagerInstance(new AddonModWorkshopPrefetchHandlerService()); - lazyHandler.setLazyInstanceMethods(['sync']); + lazyHandler.setLazyMethods(['sync']); + lazyHandler.setLazyOverrides([ + 'getFiles', + 'invalidateContent', + 'isDownloadable', + 'prefetch', + ]); return lazyHandler; } diff --git a/src/addons/mod/workshop/services/handlers/sync-cron.ts b/src/addons/mod/workshop/services/handlers/sync-cron.ts index ea7181649..0bd4b7743 100644 --- a/src/addons/mod/workshop/services/handlers/sync-cron.ts +++ b/src/addons/mod/workshop/services/handlers/sync-cron.ts @@ -39,7 +39,7 @@ export function getCronHandlerInstance(): CoreCronHandler { }); lazyHandler.setEagerInstance(new AddonModWorkshopSyncCronHandlerService()); - lazyHandler.setLazyInstanceMethods(['execute', 'getInterval']); + lazyHandler.setLazyMethods(['execute', 'getInterval']); return lazyHandler; } diff --git a/src/core/utils/async-instance.ts b/src/core/utils/async-instance.ts index b12b645d5..1517837cf 100644 --- a/src/core/utils/async-instance.ts +++ b/src/core/utils/async-instance.ts @@ -28,15 +28,19 @@ function createAsyncInstanceWrapper< lazyConstructor?: () => TLazyInstance | Promise, ): AsyncInstanceWrapper { let promisedInstance: CorePromisedValue | null = null; - let lazyInstanceMethods: Array; + let lazyMethods: Array | null = null; + let lazyOverrides: Array | null = null; let eagerInstance: TEagerInstance; return { get instance() { return promisedInstance?.value ?? undefined; }, - get lazyInstanceMethods() { - return lazyInstanceMethods; + get lazyMethods() { + return lazyMethods; + }, + get lazyOverrides() { + return lazyOverrides; }, get eagerInstance() { return eagerInstance; @@ -68,8 +72,11 @@ function createAsyncInstanceWrapper< promisedInstance.resolve(instance); }, - setLazyInstanceMethods(methods) { - lazyInstanceMethods = methods; + setLazyMethods(methods) { + lazyMethods = methods; + }, + setLazyOverrides(overrides) { + lazyOverrides = overrides; }, setEagerInstance(instance) { eagerInstance = instance; @@ -116,14 +123,16 @@ export interface AsyncInstanceWrapper< TEagerInstance extends AsyncObject = Partial > { instance?: TLazyInstance; - lazyInstanceMethods?: Array; + lazyMethods?: Array | null; + lazyOverrides?: Array | null; eagerInstance?: TEagerInstance; getInstance(): Promise; getProperty

(property: P): Promise; setInstance(instance: TLazyInstance): void; - setLazyInstanceMethods>( + setLazyMethods>( methods: LazyMethodsGuard, ): void; + setLazyOverrides(methods: Array): void; setEagerInstance(eagerInstance: TEagerInstance): void; setLazyConstructor(lazyConstructor: () => TLazyInstance | Promise): void; resetInstance(): void; @@ -156,7 +165,7 @@ export type AsyncInstance, TLazyInstance, TEagerInstance> = +export type LazyMethodsGuard, TLazyInstance, TEagerInstance> = TupleMatches> extends true ? TMethods : never; /** @@ -172,7 +181,9 @@ export function asyncInstance(lazyConstructor); return new Proxy(wrapper, { - get: (target, property, receiver) => { + get: (target, p, receiver) => { + const property = p as keyof TEagerInstance; + if (property in target) { return Reflect.get(target, property, receiver); } @@ -185,11 +196,19 @@ export function asyncInstance { expect(await asyncService.isEager()).toBe(false); }); + it('initialize instance for forced eager properties', async () => { + const asyncService = asyncInstance(() => new LazyService()); + + asyncService.setEagerInstance(new EagerService()); + asyncService.setLazyOverrides(['isEager']); + + expect(await asyncService.isEager()).toBe(false); + }); + it('does not return undefined methods when they are declared', async () => { const asyncService = asyncInstance(() => new LazyService()); asyncService.setEagerInstance(new EagerService()); - asyncService.setLazyInstanceMethods(['hello', 'goodbye']); + asyncService.setLazyMethods(['hello', 'goodbye']); expect(asyncService.hello).not.toBeUndefined(); expect(asyncService.goodbye).not.toBeUndefined();