proxy.ts
4.58 KB
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
/**
* @license
* Copyright Google Inc. 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.io/license
*/
class ProxyZoneSpec implements ZoneSpec {
name: string = 'ProxyZone';
private _delegateSpec: ZoneSpec;
properties: {[k: string]: any} = {'ProxyZoneSpec': this};
propertyKeys: string[] = null;
static get(): ProxyZoneSpec {
return Zone.current.get('ProxyZoneSpec');
}
static isLoaded(): boolean {
return ProxyZoneSpec.get() instanceof ProxyZoneSpec;
}
static assertPresent(): ProxyZoneSpec {
if (!this.isLoaded()) {
throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`);
}
return ProxyZoneSpec.get();
}
constructor(private defaultSpecDelegate: ZoneSpec = null) {
this.setDelegate(defaultSpecDelegate);
}
setDelegate(delegateSpec: ZoneSpec) {
this._delegateSpec = delegateSpec;
this.propertyKeys && this.propertyKeys.forEach((key) => delete this.properties[key]);
this.propertyKeys = null;
if (delegateSpec && delegateSpec.properties) {
this.propertyKeys = Object.keys(delegateSpec.properties);
this.propertyKeys.forEach((k) => this.properties[k] = delegateSpec.properties[k]);
}
}
getDelegate() {
return this._delegateSpec;
}
resetDelegate() {
this.setDelegate(this.defaultSpecDelegate);
}
onFork(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, zoneSpec: ZoneSpec):
Zone {
if (this._delegateSpec && this._delegateSpec.onFork) {
return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec);
} else {
return parentZoneDelegate.fork(targetZone, zoneSpec);
}
}
onIntercept(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function,
source: string): Function {
if (this._delegateSpec && this._delegateSpec.onIntercept) {
return this._delegateSpec.onIntercept(
parentZoneDelegate, currentZone, targetZone, delegate, source);
} else {
return parentZoneDelegate.intercept(targetZone, delegate, source);
}
}
onInvoke(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function,
applyThis: any, applyArgs: any[], source: string): any {
if (this._delegateSpec && this._delegateSpec.onInvoke) {
return this._delegateSpec.onInvoke(
parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source);
} else {
return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
}
}
onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any):
boolean {
if (this._delegateSpec && this._delegateSpec.onHandleError) {
return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error);
} else {
return parentZoneDelegate.handleError(targetZone, error);
}
}
onScheduleTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
Task {
if (this._delegateSpec && this._delegateSpec.onScheduleTask) {
return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task);
} else {
return parentZoneDelegate.scheduleTask(targetZone, task);
}
}
onInvokeTask(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task,
applyThis: any, applyArgs: any): any {
if (this._delegateSpec && this._delegateSpec.onFork) {
return this._delegateSpec.onInvokeTask(
parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs);
} else {
return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
}
}
onCancelTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any {
if (this._delegateSpec && this._delegateSpec.onCancelTask) {
return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task);
} else {
return parentZoneDelegate.cancelTask(targetZone, task);
}
}
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState): void {
if (this._delegateSpec && this._delegateSpec.onHasTask) {
this._delegateSpec.onHasTask(delegate, current, target, hasTaskState);
} else {
delegate.hasTask(target, hasTaskState);
}
}
}
// Export the class so that new instances can be created with proper
// constructor params.
Zone['ProxyZoneSpec'] = ProxyZoneSpec;