From 0d6eb5cb23cba7ca29186d632f5d56202c6092e5 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 26 Jul 2023 11:49:44 +0900 Subject: [PATCH] MOBILE-4272 workshop: Decouple services --- src/addons/mod/workshop/workshop.module.ts | 34 ++++++++++++------- .../components/compile-html/compile-html.ts | 15 ++++---- src/core/features/compile/services/compile.ts | 16 +++++++-- .../services/siteplugins-helper.ts | 4 ++- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/addons/mod/workshop/workshop.module.ts b/src/addons/mod/workshop/workshop.module.ts index 2a12db7d2..61314f876 100644 --- a/src/addons/mod/workshop/workshop.module.ts +++ b/src/addons/mod/workshop/workshop.module.ts @@ -23,27 +23,35 @@ import { CoreCronDelegate } from '@services/cron'; import { CORE_SITE_SCHEMAS } from '@services/sites'; import { AddonModWorkshopAssessmentStrategyModule } from './assessment/assessment.module'; import { AddonModWorkshopComponentsModule } from './components/components.module'; -import { AddonWorkshopAssessmentStrategyDelegateService } from './services/assessment-strategy-delegate'; import { ADDON_MOD_WORKSHOP_OFFLINE_SITE_SCHEMA } from './services/database/workshop'; import { AddonModWorkshopIndexLinkHandler } from './services/handlers/index-link'; import { AddonModWorkshopListLinkHandler } from './services/handlers/list-link'; import { AddonModWorkshopModuleHandler } from './services/handlers/module'; -import { AddonModWorkshopProvider } from './services/workshop'; -import { AddonModWorkshopHelperProvider } from './services/workshop-helper'; -import { AddonModWorkshopOfflineProvider } from './services/workshop-offline'; -import { AddonModWorkshopSyncProvider } from './services/workshop-sync'; import { ADDON_MOD_WORKSHOP_COMPONENT, ADDON_MOD_WORKSHOP_PAGE_NAME } from '@addons/mod/workshop/constants'; import { getCronHandlerInstance } from '@addons/mod/workshop/services/handlers/sync-cron'; import { getPrefetchHandlerInstance } from '@addons/mod/workshop/services/handlers/prefetch'; -// List of providers (without handlers). -export const ADDON_MOD_WORKSHOP_SERVICES: Type[] = [ - AddonModWorkshopProvider, - AddonModWorkshopOfflineProvider, - AddonModWorkshopSyncProvider, - AddonModWorkshopHelperProvider, - AddonWorkshopAssessmentStrategyDelegateService, -]; +/** + * Get workshop services. + * + * @returns Workshop services. + */ +export async function getWorkshopServices(): Promise[]> { + const { AddonModWorkshopProvider } = await import('@addons/mod/workshop/services/workshop'); + const { AddonModWorkshopOfflineProvider } = await import('@addons/mod/workshop/services/workshop-offline'); + const { AddonModWorkshopSyncProvider } = await import('@addons/mod/workshop/services/workshop-sync'); + const { AddonModWorkshopHelperProvider } = await import('@addons/mod/workshop/services/workshop-helper'); + const { AddonWorkshopAssessmentStrategyDelegateService } = + await import('@addons/mod/workshop/services/assessment-strategy-delegate'); + + return [ + AddonModWorkshopProvider, + AddonModWorkshopOfflineProvider, + AddonModWorkshopSyncProvider, + AddonModWorkshopHelperProvider, + AddonWorkshopAssessmentStrategyDelegateService, + ]; +} const routes: Routes = [ { diff --git a/src/core/features/compile/components/compile-html/compile-html.ts b/src/core/features/compile/components/compile-html/compile-html.ts index ecb833a32..25a937676 100644 --- a/src/core/features/compile/components/compile-html/compile-html.ts +++ b/src/core/features/compile/components/compile-html/compile-html.ts @@ -126,11 +126,8 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck { this.compiling.emit(true); try { - const factory = await CoreCompile.createAndCompileComponent( - this.text, - this.getComponentClass(), - this.extraImports, - ); + const componentClass = await this.getComponentClass(); + const factory = await CoreCompile.createAndCompileComponent(this.text, componentClass, this.extraImports); // Destroy previous components. this.componentRef?.destroy(); @@ -166,9 +163,10 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck { * * @returns The component class. */ - protected getComponentClass(): Type { + protected async getComponentClass(): Promise> { // eslint-disable-next-line @typescript-eslint/no-this-alias const compileInstance = this; + const lazyLibraries = await CoreCompile.getLazyLibraries(); // Create the component, using the text as the template. return class CoreCompileHtmlFakeComponent implements OnInit, AfterContentInit, AfterViewInit, OnDestroy { @@ -184,7 +182,10 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck { this['dataArray'] = []; // Inject the libraries. - CoreCompile.injectLibraries(this, compileInstance.extraProviders); + CoreCompile.injectLibraries(this, [ + ...lazyLibraries, + ...compileInstance.extraProviders, + ]); // Always add these elements, they could be needed on component init (componentObservable). this['ChangeDetectorRef'] = compileInstance.changeDetector; diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index 30eb5d31b..36631c2b8 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -149,7 +149,7 @@ import { ADDON_MOD_SCORM_SERVICES } from '@addons/mod/scorm/scorm.module'; import { ADDON_MOD_SURVEY_SERVICES } from '@addons/mod/survey/survey.module'; import { ADDON_MOD_URL_SERVICES } from '@addons/mod/url/url.module'; import { ADDON_MOD_WIKI_SERVICES } from '@addons/mod/wiki/wiki.module'; -import { ADDON_MOD_WORKSHOP_SERVICES } from '@addons/mod/workshop/workshop.module'; +import { getWorkshopServices } from '@addons/mod/workshop/workshop.module'; import { ADDON_NOTES_SERVICES } from '@addons/notes/notes.module'; import { ADDON_NOTIFICATIONS_SERVICES } from '@addons/notifications/notifications.module'; import { ADDON_PRIVATEFILES_SERVICES } from '@addons/privatefiles/privatefiles.module'; @@ -317,7 +317,6 @@ export class CoreCompileProvider { ...ADDON_MOD_SURVEY_SERVICES, ...ADDON_MOD_URL_SERVICES, ...ADDON_MOD_WIKI_SERVICES, - ...ADDON_MOD_WORKSHOP_SERVICES, ...ADDON_NOTES_SERVICES, ...ADDON_NOTIFICATIONS_SERVICES, ...ADDON_PRIVATEFILES_SERVICES, @@ -388,6 +387,19 @@ export class CoreCompileProvider { }); } + /** + * Get lazy libraries to inject. + * + * @returns Lazy libraries. + */ + async getLazyLibraries(): Promise[]> { + const ADDON_MOD_WORKSHOP_SERVICES = await getWorkshopServices(); + + return [ + ...ADDON_MOD_WORKSHOP_SERVICES, + ]; + } + /** * Instantiate a dynamic component. * diff --git a/src/core/features/siteplugins/services/siteplugins-helper.ts b/src/core/features/siteplugins/services/siteplugins-helper.ts index 93e96edda..37759ab91 100644 --- a/src/core/features/siteplugins/services/siteplugins-helper.ts +++ b/src/core/features/siteplugins/services/siteplugins-helper.ts @@ -268,11 +268,13 @@ export class CoreSitePluginsHelperProvider { } // Create a "fake" instance to hold all the libraries. + const lazyLibraries = await CoreCompile.getLazyLibraries(); const instance = { // eslint-disable-next-line @typescript-eslint/naming-convention HANDLER_DISABLED: HANDLER_DISABLED, }; - CoreCompile.injectLibraries(instance); + + CoreCompile.injectLibraries(instance, lazyLibraries); // Add some data of the WS call result. const jsData = CoreSitePlugins.createDataForJS(result);