-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathng_module_registration.ts
66 lines (58 loc) · 2.05 KB
/
ng_module_registration.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
/**
* @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 {Type} from '../interface/type';
import {NgModuleType} from '../metadata/ng_module_def';
import {stringify} from '../util/stringify';
/**
* Map of module-id to the corresponding NgModule.
*/
const modules = new Map<string, NgModuleType>();
/**
* Whether to check for duplicate NgModule registrations.
*
* This can be disabled for testing.
*/
let checkForDuplicateNgModules = true;
function assertSameOrNotExisting(id: string, type: Type<any> | null, incoming: Type<any>): void {
if (type && type !== incoming && checkForDuplicateNgModules) {
throw new Error(
`Duplicate module registered for ${id} - ${stringify(type)} vs ${stringify(type.name)}`,
);
}
}
/**
* Adds the given NgModule type to Angular's NgModule registry.
*
* This is generated as a side-effect of NgModule compilation. Note that the `id` is passed in
* explicitly and not read from the NgModule definition. This is for two reasons: it avoids a
* megamorphic read, and in JIT there's a chicken-and-egg problem where the NgModule may not be
* fully resolved when it's registered.
*
* @codeGenApi
*/
export function registerNgModuleType(ngModuleType: NgModuleType, id: string): void {
const existing = modules.get(id) || null;
assertSameOrNotExisting(id, existing, ngModuleType);
modules.set(id, ngModuleType);
}
export function clearModulesForTest(): void {
modules.clear();
}
export function getRegisteredNgModuleType(id: string): NgModuleType | undefined {
return modules.get(id);
}
/**
* Control whether the NgModule registration system enforces that each NgModule type registered has
* a unique id.
*
* This is useful for testing as the NgModule registry cannot be properly reset between tests with
* Angular's current API.
*/
export function setAllowDuplicateNgModuleIdsForTest(allowDuplicates: boolean): void {
checkForDuplicateNgModules = !allowDuplicates;
}