-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathplatform_ref.ts
161 lines (149 loc) Β· 5.26 KB
/
platform_ref.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @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 {compileNgModuleFactory} from '../application/application_ngmodule_factory_compiler';
import {BootstrapOptions, optionsReducer} from '../application/application_ref';
import {
getNgZoneOptions,
internalProvideZoneChangeDetection,
} from '../change_detection/scheduling/ng_zone_scheduling';
import {ChangeDetectionScheduler} from '../change_detection/scheduling/zoneless_scheduling';
import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl';
import {Injectable, Injector} from '../di';
import {errorHandlerEnvironmentInitializer} from '../error_handler';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';
import {CompilerOptions} from '../linker';
import {NgModuleFactory, NgModuleRef} from '../linker/ng_module_factory';
import {createNgModuleRefWithProviders} from '../render3/ng_module_ref';
import {getNgZone} from '../zone/ng_zone';
import {bootstrap, setModuleBootstrapImpl} from './bootstrap';
import {PLATFORM_DESTROY_LISTENERS} from './platform_destroy_listeners';
/**
* The Angular platform is the entry point for Angular on a web page.
* Each page has exactly one platform. Services (such as reflection) which are common
* to every Angular application running on the page are bound in its scope.
* A page's platform is initialized implicitly when a platform is created using a platform
* factory such as `PlatformBrowser`, or explicitly by calling the `createPlatform()` function.
*
* @publicApi
*/
@Injectable({providedIn: 'platform'})
export class PlatformRef {
private _modules: NgModuleRef<any>[] = [];
private _destroyListeners: Array<() => void> = [];
private _destroyed: boolean = false;
/** @internal */
constructor(private _injector: Injector) {}
/**
* Creates an instance of an `@NgModule` for the given platform.
*
* @deprecated Passing NgModule factories as the `PlatformRef.bootstrapModuleFactory` function
* argument is deprecated. Use the `PlatformRef.bootstrapModule` API instead.
*/
bootstrapModuleFactory<M>(
moduleFactory: NgModuleFactory<M>,
options?: BootstrapOptions,
): Promise<NgModuleRef<M>> {
const scheduleInRootZone = (options as any)?.scheduleInRootZone;
const ngZoneFactory = () =>
getNgZone(options?.ngZone, {
...getNgZoneOptions({
eventCoalescing: options?.ngZoneEventCoalescing,
runCoalescing: options?.ngZoneRunCoalescing,
}),
scheduleInRootZone,
});
const ignoreChangesOutsideZone = options?.ignoreChangesOutsideZone;
const allAppProviders = [
internalProvideZoneChangeDetection({
ngZoneFactory,
ignoreChangesOutsideZone,
}),
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
errorHandlerEnvironmentInitializer,
];
const moduleRef = createNgModuleRefWithProviders(
moduleFactory.moduleType,
this.injector,
allAppProviders,
);
setModuleBootstrapImpl();
return bootstrap({
moduleRef,
allPlatformModules: this._modules,
platformInjector: this.injector,
});
}
/**
* Creates an instance of an `@NgModule` for a given platform.
*
* @usageNotes
* ### Simple Example
*
* ```ts
* @NgModule({
* imports: [BrowserModule]
* })
* class MyModule {}
*
* let moduleRef = platformBrowser().bootstrapModule(MyModule);
* ```
*
*/
bootstrapModule<M>(
moduleType: Type<M>,
compilerOptions:
| (CompilerOptions & BootstrapOptions)
| Array<CompilerOptions & BootstrapOptions> = [],
): Promise<NgModuleRef<M>> {
const options = optionsReducer({}, compilerOptions);
setModuleBootstrapImpl();
return compileNgModuleFactory(this.injector, options, moduleType).then((moduleFactory) =>
this.bootstrapModuleFactory(moduleFactory, options),
);
}
/**
* Registers a listener to be called when the platform is destroyed.
*/
onDestroy(callback: () => void): void {
this._destroyListeners.push(callback);
}
/**
* Retrieves the platform {@link Injector}, which is the parent injector for
* every Angular application on the page and provides singleton providers.
*/
get injector(): Injector {
return this._injector;
}
/**
* Destroys the current Angular platform and all Angular applications on the page.
* Destroys all modules and listeners registered with the platform.
*/
destroy() {
if (this._destroyed) {
throw new RuntimeError(
RuntimeErrorCode.PLATFORM_ALREADY_DESTROYED,
ngDevMode && 'The platform has already been destroyed!',
);
}
this._modules.slice().forEach((module) => module.destroy());
this._destroyListeners.forEach((listener) => listener());
const destroyListeners = this._injector.get(PLATFORM_DESTROY_LISTENERS, null);
if (destroyListeners) {
destroyListeners.forEach((listener) => listener());
destroyListeners.clear();
}
this._destroyed = true;
}
/**
* Indicates whether this instance was destroyed.
*/
get destroyed() {
return this._destroyed;
}
}