Skip to content

Commit b39dc1b

Browse files
committed
MOBILE-4529 addons: Fix lazy instances overrides
1 parent 69cf1ea commit b39dc1b

File tree

10 files changed

+61
-20
lines changed

10 files changed

+61
-20
lines changed

src/addons/mod/survey/services/handlers/prefetch.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
5151
});
5252

5353
prefetchHandlerInstance.setEagerInstance(new AddonModSurveyPrefetchHandlerService());
54-
prefetchHandlerInstance.setLazyInstanceMethods(['sync']);
54+
prefetchHandlerInstance.setLazyMethods(['sync']);
55+
prefetchHandlerInstance.setLazyOverrides([
56+
'prefetch',
57+
'isEnabled',
58+
'invalidateModule',
59+
'invalidateContent',
60+
'getIntroFiles',
61+
]);
5562
}
5663

5764
return prefetchHandlerInstance;

src/addons/mod/survey/services/handlers/sync-cron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function getCronHandlerInstance(): CoreCronHandler {
3939
});
4040

4141
lazyHandler.setEagerInstance(new AddonModSurveySyncCronHandlerService());
42-
lazyHandler.setLazyInstanceMethods(['execute', 'getInterval']);
42+
lazyHandler.setLazyMethods(['execute', 'getInterval']);
4343

4444
return lazyHandler;
4545
}

src/addons/mod/workshop/assessment/accumulative/services/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS
5151
});
5252

5353
lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyAccumulativeHandlerService());
54-
lazyHandler.setLazyInstanceMethods([
54+
lazyHandler.setLazyMethods([
5555
'getComponent',
5656
'getOriginalValues',
5757
'hasDataChanged',

src/addons/mod/workshop/assessment/comments/services/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS
5151
});
5252

5353
lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyCommentsHandlerService());
54-
lazyHandler.setLazyInstanceMethods([
54+
lazyHandler.setLazyMethods([
5555
'getComponent',
5656
'getOriginalValues',
5757
'hasDataChanged',

src/addons/mod/workshop/assessment/numerrors/services/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS
5151
});
5252

5353
lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyNumErrorsHandlerService());
54-
lazyHandler.setLazyInstanceMethods([
54+
lazyHandler.setLazyMethods([
5555
'getComponent',
5656
'getOriginalValues',
5757
'hasDataChanged',

src/addons/mod/workshop/assessment/rubric/services/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentS
5151
});
5252

5353
lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyRubricHandlerService());
54-
lazyHandler.setLazyInstanceMethods([
54+
lazyHandler.setLazyMethods([
5555
'getComponent',
5656
'getOriginalValues',
5757
'hasDataChanged',

src/addons/mod/workshop/services/handlers/prefetch.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
4848
});
4949

5050
lazyHandler.setEagerInstance(new AddonModWorkshopPrefetchHandlerService());
51-
lazyHandler.setLazyInstanceMethods(['sync']);
51+
lazyHandler.setLazyMethods(['sync']);
52+
lazyHandler.setLazyOverrides([
53+
'getFiles',
54+
'invalidateContent',
55+
'isDownloadable',
56+
'prefetch',
57+
]);
5258

5359
return lazyHandler;
5460
}

src/addons/mod/workshop/services/handlers/sync-cron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function getCronHandlerInstance(): CoreCronHandler {
3939
});
4040

4141
lazyHandler.setEagerInstance(new AddonModWorkshopSyncCronHandlerService());
42-
lazyHandler.setLazyInstanceMethods(['execute', 'getInterval']);
42+
lazyHandler.setLazyMethods(['execute', 'getInterval']);
4343

4444
return lazyHandler;
4545
}

src/core/utils/async-instance.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ function createAsyncInstanceWrapper<
2828
lazyConstructor?: () => TLazyInstance | Promise<TLazyInstance>,
2929
): AsyncInstanceWrapper<TLazyInstance, TEagerInstance> {
3030
let promisedInstance: CorePromisedValue<TLazyInstance> | null = null;
31-
let lazyInstanceMethods: Array<string | symbol>;
31+
let lazyMethods: Array<string | number | symbol> | null = null;
32+
let lazyOverrides: Array<keyof TEagerInstance> | null = null;
3233
let eagerInstance: TEagerInstance;
3334

3435
return {
3536
get instance() {
3637
return promisedInstance?.value ?? undefined;
3738
},
38-
get lazyInstanceMethods() {
39-
return lazyInstanceMethods;
39+
get lazyMethods() {
40+
return lazyMethods;
41+
},
42+
get lazyOverrides() {
43+
return lazyOverrides;
4044
},
4145
get eagerInstance() {
4246
return eagerInstance;
@@ -68,8 +72,11 @@ function createAsyncInstanceWrapper<
6872

6973
promisedInstance.resolve(instance);
7074
},
71-
setLazyInstanceMethods(methods) {
72-
lazyInstanceMethods = methods;
75+
setLazyMethods(methods) {
76+
lazyMethods = methods;
77+
},
78+
setLazyOverrides(overrides) {
79+
lazyOverrides = overrides;
7380
},
7481
setEagerInstance(instance) {
7582
eagerInstance = instance;
@@ -116,14 +123,16 @@ export interface AsyncInstanceWrapper<
116123
TEagerInstance extends AsyncObject = Partial<TLazyInstance>
117124
> {
118125
instance?: TLazyInstance;
119-
lazyInstanceMethods?: Array<string | symbol>;
126+
lazyMethods?: Array<string | number | symbol> | null;
127+
lazyOverrides?: Array<keyof TEagerInstance> | null;
120128
eagerInstance?: TEagerInstance;
121129
getInstance(): Promise<TLazyInstance>;
122130
getProperty<P extends keyof TLazyInstance>(property: P): Promise<TLazyInstance[P]>;
123131
setInstance(instance: TLazyInstance): void;
124-
setLazyInstanceMethods<const T extends Array<string | symbol>>(
132+
setLazyMethods<const T extends Array<string | number | symbol>>(
125133
methods: LazyMethodsGuard<T, TLazyInstance, TEagerInstance>,
126134
): void;
135+
setLazyOverrides(methods: Array<keyof TEagerInstance>): void;
127136
setEagerInstance(eagerInstance: TEagerInstance): void;
128137
setLazyConstructor(lazyConstructor: () => TLazyInstance | Promise<TLazyInstance>): void;
129138
resetInstance(): void;
@@ -156,7 +165,7 @@ export type AsyncInstance<TLazyInstance extends TEagerInstance, TEagerInstance e
156165
/**
157166
* Guard type to make sure that lazy methods match what the lazy class implements.
158167
*/
159-
export type LazyMethodsGuard<TMethods extends Array<string | symbol>, TLazyInstance, TEagerInstance> =
168+
export type LazyMethodsGuard<TMethods extends Array<string | number | symbol>, TLazyInstance, TEagerInstance> =
160169
TupleMatches<TMethods, Exclude<keyof TLazyInstance, keyof TEagerInstance>> extends true ? TMethods : never;
161170

162171
/**
@@ -172,7 +181,9 @@ export function asyncInstance<TLazyInstance extends TEagerInstance, TEagerInstan
172181
const wrapper = createAsyncInstanceWrapper<TLazyInstance, TEagerInstance>(lazyConstructor);
173182

174183
return new Proxy(wrapper, {
175-
get: (target, property, receiver) => {
184+
get: (target, p, receiver) => {
185+
const property = p as keyof TEagerInstance;
186+
176187
if (property in target) {
177188
return Reflect.get(target, property, receiver);
178189
}
@@ -185,11 +196,19 @@ export function asyncInstance<TLazyInstance extends TEagerInstance, TEagerInstan
185196
: value;
186197
}
187198

188-
if (wrapper.eagerInstance && property in wrapper.eagerInstance) {
199+
if (
200+
wrapper.eagerInstance &&
201+
property in wrapper.eagerInstance &&
202+
!wrapper.lazyOverrides?.includes(property)
203+
) {
189204
return Reflect.get(wrapper.eagerInstance, property, receiver);
190205
}
191206

192-
if (wrapper.lazyInstanceMethods && !wrapper.lazyInstanceMethods.includes(property)) {
207+
if (
208+
wrapper.lazyMethods &&
209+
!wrapper.lazyMethods.includes(property) &&
210+
!wrapper.lazyOverrides?.includes(property)
211+
) {
193212
return undefined;
194213
}
195214

src/core/utils/tests/async-instance.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ describe('AsyncInstance', () => {
3939
expect(await asyncService.isEager()).toBe(false);
4040
});
4141

42+
it('initialize instance for forced eager properties', async () => {
43+
const asyncService = asyncInstance(() => new LazyService());
44+
45+
asyncService.setEagerInstance(new EagerService());
46+
asyncService.setLazyOverrides(['isEager']);
47+
48+
expect(await asyncService.isEager()).toBe(false);
49+
});
50+
4251
it('does not return undefined methods when they are declared', async () => {
4352
const asyncService = asyncInstance<LazyService, EagerService>(() => new LazyService());
4453

4554
asyncService.setEagerInstance(new EagerService());
46-
asyncService.setLazyInstanceMethods(['hello', 'goodbye']);
55+
asyncService.setLazyMethods(['hello', 'goodbye']);
4756

4857
expect(asyncService.hello).not.toBeUndefined();
4958
expect(asyncService.goodbye).not.toBeUndefined();

0 commit comments

Comments
 (0)