X Tutup
Skip to content

Commit 8dd3082

Browse files
committed
feat(core): PlatformRef and ApplicationRef support registration of disposal functions.
These functions will be called whenever the platform or application are being disposed.
1 parent b2dc5c2 commit 8dd3082

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

modules/angular2/src/core/application_ref.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ export function platformCommon(bindings?: Array<Type | Provider | any[]>, initia
150150
* explicitly by calling {@link platform}().
151151
*/
152152
export abstract class PlatformRef {
153+
/**
154+
* Register a listener to be called when the platform is disposed.
155+
*/
156+
abstract registerDisposeListener(dispose: () => void): void;
157+
153158
/**
154159
* Retrieve the platform {@link Injector}, which is the parent injector for
155160
* every Angular application on the page and provides singleton providers.
@@ -210,9 +215,12 @@ export abstract class PlatformRef {
210215
export class PlatformRef_ extends PlatformRef {
211216
/** @internal */
212217
_applications: ApplicationRef[] = [];
218+
_disposeListeners: Function[] = [];
213219

214220
constructor(private _injector: Injector, private _dispose: () => void) { super(); }
215221

222+
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
223+
216224
get injector(): Injector { return this._injector; }
217225

218226
application(bindings: Array<Type | Provider | any[]>): ApplicationRef {
@@ -259,6 +267,7 @@ export class PlatformRef_ extends PlatformRef {
259267

260268
dispose(): void {
261269
this._applications.forEach((app) => app.dispose());
270+
this._disposeListeners.forEach((dispose) => dispose());
262271
this._dispose();
263272
}
264273

@@ -278,6 +287,11 @@ export abstract class ApplicationRef {
278287
*/
279288
abstract registerBootstrapListener(listener: (ref: ComponentRef) => void): void;
280289

290+
/**
291+
* Register a listener to be called when the application is disposed.
292+
*/
293+
abstract registerDisposeListener(dispose: () => void): void;
294+
281295
/**
282296
* Bootstrap a new component at the root level of the application.
283297
*
@@ -326,6 +340,7 @@ export abstract class ApplicationRef {
326340

327341
export class ApplicationRef_ extends ApplicationRef {
328342
private _bootstrapListeners: Function[] = [];
343+
private _disposeListeners: Function[] = [];
329344
private _rootComponents: ComponentRef[] = [];
330345
private _rootComponentTypes: Type[] = [];
331346

@@ -337,6 +352,8 @@ export class ApplicationRef_ extends ApplicationRef {
337352
this._bootstrapListeners.push(listener);
338353
}
339354

355+
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
356+
340357
bootstrap(componentType: Type,
341358
providers?: Array<Type | Provider | any[]>): Promise<ComponentRef> {
342359
var completer = PromiseWrapper.completer();
@@ -380,6 +397,7 @@ export class ApplicationRef_ extends ApplicationRef {
380397
dispose(): void {
381398
// TODO(alxhub): Dispose of the NgZone.
382399
this._rootComponents.forEach((ref) => ref.dispose());
400+
this._disposeListeners.forEach((dispose) => dispose());
383401
this._platform._applicationDisposed(this);
384402
}
385403

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {ApplicationRef} from 'angular2/src/core/application_ref';
2+
import {Type} from 'angular2/src/core/facade/lang';
3+
import {ComponentRef} from 'angular2/src/core/linker/dynamic_component_loader';
4+
import {Provider, Injector} from 'angular2/src/core/di';
5+
import {NgZone} from 'angular2/src/core/zone/ng_zone';
6+
import {Promise} from 'angular2/src/core/facade/async';
7+
8+
export class MockApplicationRef extends ApplicationRef {
9+
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {}
10+
11+
registerDisposeListener(dispose: () => void): void {}
12+
13+
bootstrap(componentType: Type, bindings?: Array<Type | Provider | any[]>): Promise<ComponentRef> {
14+
return null;
15+
}
16+
17+
get injector(): Injector { return null; };
18+
19+
get zone(): NgZone { return null; };
20+
21+
dispose(): void {}
22+
23+
get componentTypes(): Type[] { return null; };
24+
}

modules/angular2/test/public_api_spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var NG_API = [
9898
'ApplicationRef.bootstrap()',
9999
'ApplicationRef.dispose()',
100100
'ApplicationRef.registerBootstrapListener()',
101+
'ApplicationRef.registerDisposeListener()',
101102
*/
102103
'AsyncPipe',
103104
'AsyncPipe.onDestroy()',
@@ -873,6 +874,7 @@ var NG_API = [
873874
'PlatformRef.application()',
874875
'PlatformRef.asyncApplication()',
875876
'PlatformRef.dispose()',
877+
'PlatformRef.registerDisposeListener()',
876878
*/
877879
'PlatformRef.injector',
878880
'Predicate:dart',

0 commit comments

Comments
 (0)
X Tutup