|
| 1 | +(function () { |
| 2 | + class ProxyZoneSpec implements ZoneSpec { |
| 3 | + name: string = 'ProxyZone'; |
| 4 | + |
| 5 | + private _delegateSpec: ZoneSpec; |
| 6 | + |
| 7 | + properties: {[k: string]: any} = {'ProxyZoneSpec': this}; |
| 8 | + propertyKeys: string[] = null; |
| 9 | + |
| 10 | + static get(): ProxyZoneSpec { |
| 11 | + return Zone.current.get('ProxyZoneSpec'); |
| 12 | + } |
| 13 | + |
| 14 | + static isLoaded(): boolean { |
| 15 | + return ProxyZoneSpec.get() instanceof ProxyZoneSpec; |
| 16 | + } |
| 17 | + |
| 18 | + static assertPresent(): ProxyZoneSpec { |
| 19 | + if (!this.isLoaded()) { |
| 20 | + throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`); |
| 21 | + } |
| 22 | + return ProxyZoneSpec.get(); |
| 23 | + } |
| 24 | + |
| 25 | + constructor(private defaultSpecDelegate: ZoneSpec = null) { |
| 26 | + this.setDelegate(defaultSpecDelegate); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + setDelegate(delegateSpec: ZoneSpec) { |
| 31 | + this._delegateSpec = delegateSpec; |
| 32 | + this.propertyKeys && this.propertyKeys.forEach((key) => delete this.properties[key]); |
| 33 | + this.propertyKeys = null; |
| 34 | + if (delegateSpec && delegateSpec.properties) { |
| 35 | + this.propertyKeys = Object.keys(delegateSpec.properties); |
| 36 | + this.propertyKeys.forEach((k) => this.properties[k] = delegateSpec.properties[k]); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + getDelegate() { |
| 41 | + return this._delegateSpec; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + resetDelegate() { |
| 46 | + this.setDelegate(this.defaultSpecDelegate); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + onFork(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 51 | + zoneSpec: ZoneSpec): Zone { |
| 52 | + if (this._delegateSpec && this._delegateSpec.onFork) { |
| 53 | + return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec); |
| 54 | + } else { |
| 55 | + return parentZoneDelegate.fork(targetZone, zoneSpec); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + onIntercept(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 61 | + delegate: Function, source: string): Function { |
| 62 | + if (this._delegateSpec && this._delegateSpec.onIntercept) { |
| 63 | + return this._delegateSpec.onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source); |
| 64 | + } else { |
| 65 | + return parentZoneDelegate.intercept(targetZone, delegate, source); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 71 | + delegate: Function, applyThis: any, applyArgs: any[], source: string): any { |
| 72 | + if (this._delegateSpec && this._delegateSpec.onInvoke) { |
| 73 | + return this._delegateSpec.onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source); |
| 74 | + } else { |
| 75 | + return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 80 | + error: any): boolean { |
| 81 | + if (this._delegateSpec && this._delegateSpec.onHandleError) { |
| 82 | + return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error); |
| 83 | + } else { |
| 84 | + return parentZoneDelegate.handleError(targetZone, error); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + onScheduleTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 89 | + task: Task): Task { |
| 90 | + if (this._delegateSpec && this._delegateSpec.onScheduleTask) { |
| 91 | + return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task); |
| 92 | + } else { |
| 93 | + return parentZoneDelegate.scheduleTask(targetZone, task); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + onInvokeTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 98 | + task: Task, applyThis: any, applyArgs: any): any { |
| 99 | + if (this._delegateSpec && this._delegateSpec.onFork) { |
| 100 | + return this._delegateSpec.onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs); |
| 101 | + } else { |
| 102 | + return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + onCancelTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 107 | + task: Task): any { |
| 108 | + if (this._delegateSpec && this._delegateSpec.onCancelTask) { |
| 109 | + return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task); |
| 110 | + } else { |
| 111 | + return parentZoneDelegate.cancelTask(targetZone, task); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, |
| 116 | + hasTaskState: HasTaskState): void { |
| 117 | + if (this._delegateSpec && this._delegateSpec.onHasTask) { |
| 118 | + this._delegateSpec.onHasTask(delegate, current, target, hasTaskState); |
| 119 | + } else { |
| 120 | + delegate.hasTask(target, hasTaskState); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // Export the class so that new instances can be created with proper |
| 126 | + // constructor params. |
| 127 | + Zone['ProxyZoneSpec'] = ProxyZoneSpec; |
| 128 | +})(); |
0 commit comments