forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached_injector_service.ts
59 lines (54 loc) · 1.75 KB
/
cached_injector_service.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {ɵɵdefineInjectable as defineInjectable} from './di/interface/defs';
import {Provider} from './di/interface/provider';
import {EnvironmentInjector} from './di/r3_injector';
import {OnDestroy} from './interface/lifecycle_hooks';
import {createEnvironmentInjector} from './render3/ng_module_ref';
/**
* A service used by the framework to create and cache injector instances.
*
* This service is used to create a single injector instance for each defer
* block definition, to avoid creating an injector for each defer block instance
* of a certain type.
*/
export class CachedInjectorService implements OnDestroy {
private cachedInjectors = new Map<unknown, EnvironmentInjector | null>();
getOrCreateInjector(
key: unknown,
parentInjector: EnvironmentInjector,
providers: Provider[],
debugName?: string,
) {
if (!this.cachedInjectors.has(key)) {
const injector =
providers.length > 0
? createEnvironmentInjector(providers, parentInjector, debugName)
: null;
this.cachedInjectors.set(key, injector);
}
return this.cachedInjectors.get(key)!;
}
ngOnDestroy() {
try {
for (const injector of this.cachedInjectors.values()) {
if (injector !== null) {
injector.destroy();
}
}
} finally {
this.cachedInjectors.clear();
}
}
/** @nocollapse */
static ɵprov = /** @pureOrBreakMyCode */ /* @__PURE__ */ defineInjectable({
token: CachedInjectorService,
providedIn: 'environment',
factory: () => new CachedInjectorService(),
});
}