-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSingletonLazyAppServicesContainer.ts
72 lines (57 loc) · 1.7 KB
/
SingletonLazyAppServicesContainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { AppServices } from "../abstractions";
export type ServicesFactories = {
[Property in keyof AppServices as `${string & Property}Factory`]: (
appServices: AppServices,
) => AppServices[Property];
};
export class SingletonLazyAppServicesContainer implements AppServices {
private readonly _instances: Partial<AppServices> = {};
private readonly _factories: ServicesFactories;
constructor(factories: ServicesFactories) {
this._factories = { ...factories };
}
private singleton<N extends keyof AppServices, T extends AppServices[N]>(
name: N,
): T {
if (!this._instances[name]) {
this._instances[name] = this._factories[`${name}Factory`](
this,
) as unknown as T;
}
return this._instances[name] as T;
}
// TODO: generate these properties automatically
get window() {
return this.singleton("window");
}
get appConfiguration() {
return this.singleton("appConfiguration");
}
get appConfigurationRenderer() {
return this.singleton("appConfigurationRenderer");
}
get appSessionStateAccessor() {
return this.singleton("appSessionStateAccessor");
}
get appSessionStateMonitor() {
return this.singleton("appSessionStateMonitor");
}
get axiosStatic() {
return this.singleton("axiosStatic");
}
get htmlEditorApiClient() {
return this.singleton("htmlEditorApiClient");
}
get dopplerRestApiClient() {
return this.singleton("dopplerRestApiClient");
}
get dopplerLegacyClient() {
return this.singleton("dopplerLegacyClient");
}
get assetManifestClient() {
return this.singleton("assetManifestClient");
}
get editorExtensionsBridge() {
return this.singleton("editorExtensionsBridge");
}
}