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();