|
1 |
| -(function() { |
2 |
| - class AsyncTestZoneSpec implements ZoneSpec { |
3 |
| - _finishCallback: Function; |
4 |
| - _failCallback: Function; |
5 |
| - _pendingMicroTasks: boolean = false; |
6 |
| - _pendingMacroTasks: boolean = false; |
7 |
| - _alreadyErrored: boolean = false; |
8 |
| - runZone = Zone.current; |
| 1 | +class AsyncTestZoneSpec implements ZoneSpec { |
| 2 | + _finishCallback: Function; |
| 3 | + _failCallback: Function; |
| 4 | + _pendingMicroTasks: boolean = false; |
| 5 | + _pendingMacroTasks: boolean = false; |
| 6 | + _alreadyErrored: boolean = false; |
| 7 | + runZone = Zone.current; |
9 | 8 |
|
10 |
| - constructor(finishCallback: Function, failCallback: Function, namePrefix: string) { |
11 |
| - this._finishCallback = finishCallback; |
12 |
| - this._failCallback = failCallback; |
13 |
| - this.name = 'asyncTestZone for ' + namePrefix; |
14 |
| - } |
| 9 | + constructor(finishCallback: Function, failCallback: Function, namePrefix: string) { |
| 10 | + this._finishCallback = finishCallback; |
| 11 | + this._failCallback = failCallback; |
| 12 | + this.name = 'asyncTestZone for ' + namePrefix; |
| 13 | + } |
15 | 14 |
|
16 |
| - _finishCallbackIfDone() { |
17 |
| - if (!(this._pendingMicroTasks || this._pendingMacroTasks)) { |
18 |
| - // We do this because we would like to catch unhandled rejected promises. |
19 |
| - this.runZone.run(() => { |
20 |
| - setTimeout(() => { |
21 |
| - if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) { |
22 |
| - this._finishCallback(); |
23 |
| - } |
24 |
| - }, 0); |
25 |
| - }); |
26 |
| - } |
| 15 | + _finishCallbackIfDone() { |
| 16 | + if (!(this._pendingMicroTasks || this._pendingMacroTasks)) { |
| 17 | + // We do this because we would like to catch unhandled rejected promises. |
| 18 | + this.runZone.run(() => { |
| 19 | + setTimeout(() => { |
| 20 | + if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) { |
| 21 | + this._finishCallback(); |
| 22 | + } |
| 23 | + }, 0); |
| 24 | + }); |
27 | 25 | }
|
| 26 | + } |
28 | 27 |
|
29 |
| - // ZoneSpec implementation below. |
| 28 | + // ZoneSpec implementation below. |
30 | 29 |
|
31 |
| - name: string; |
| 30 | + name: string; |
32 | 31 |
|
33 |
| - // Note - we need to use onInvoke at the moment to call finish when a test is |
34 |
| - // fully synchronous. TODO(juliemr): remove this when the logic for |
35 |
| - // onHasTask changes and it calls whenever the task queues are dirty. |
36 |
| - onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
37 |
| - delegate: Function, applyThis: any, applyArgs: any[], source: string): any { |
38 |
| - try { |
39 |
| - return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source); |
40 |
| - } finally { |
41 |
| - this._finishCallbackIfDone(); |
42 |
| - } |
| 32 | + // Note - we need to use onInvoke at the moment to call finish when a test is |
| 33 | + // fully synchronous. TODO(juliemr): remove this when the logic for |
| 34 | + // onHasTask changes and it calls whenever the task queues are dirty. |
| 35 | + onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 36 | + delegate: Function, applyThis: any, applyArgs: any[], source: string): any { |
| 37 | + try { |
| 38 | + return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source); |
| 39 | + } finally { |
| 40 | + this._finishCallbackIfDone(); |
43 | 41 | }
|
| 42 | + } |
44 | 43 |
|
45 |
| - onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
46 |
| - error: any): boolean { |
47 |
| - // Let the parent try to handle the error. |
48 |
| - const result = parentZoneDelegate.handleError(targetZone, error); |
49 |
| - if (result) { |
50 |
| - this._failCallback(error); |
51 |
| - this._alreadyErrored = true; |
52 |
| - } |
53 |
| - return false; |
| 44 | + onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, |
| 45 | + error: any): boolean { |
| 46 | + // Let the parent try to handle the error. |
| 47 | + const result = parentZoneDelegate.handleError(targetZone, error); |
| 48 | + if (result) { |
| 49 | + this._failCallback(error); |
| 50 | + this._alreadyErrored = true; |
54 | 51 | }
|
| 52 | + return false; |
| 53 | + } |
55 | 54 |
|
56 |
| - onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task { |
57 |
| - if (task.type == 'macroTask' && task.source == 'setInterval') { |
58 |
| - this._failCallback('Cannot use setInterval from within an async zone test.'); |
59 |
| - return; |
60 |
| - } |
61 |
| - |
62 |
| - return delegate.scheduleTask(targetZone, task); |
| 55 | + onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task { |
| 56 | + if (task.type == 'macroTask' && task.source == 'setInterval') { |
| 57 | + this._failCallback('Cannot use setInterval from within an async zone test.'); |
| 58 | + return; |
63 | 59 | }
|
64 | 60 |
|
65 |
| - onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) { |
66 |
| - delegate.hasTask(target, hasTaskState); |
67 |
| - if (hasTaskState.change == 'microTask') { |
68 |
| - this._pendingMicroTasks = hasTaskState.microTask; |
69 |
| - this._finishCallbackIfDone(); |
70 |
| - } else if (hasTaskState.change == 'macroTask') { |
71 |
| - this._pendingMacroTasks = hasTaskState.macroTask; |
72 |
| - this._finishCallbackIfDone(); |
73 |
| - } |
| 61 | + return delegate.scheduleTask(targetZone, task); |
| 62 | + } |
| 63 | + |
| 64 | + onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) { |
| 65 | + delegate.hasTask(target, hasTaskState); |
| 66 | + if (hasTaskState.change == 'microTask') { |
| 67 | + this._pendingMicroTasks = hasTaskState.microTask; |
| 68 | + this._finishCallbackIfDone(); |
| 69 | + } else if (hasTaskState.change == 'macroTask') { |
| 70 | + this._pendingMacroTasks = hasTaskState.macroTask; |
| 71 | + this._finishCallbackIfDone(); |
74 | 72 | }
|
75 | 73 | }
|
| 74 | +} |
76 | 75 |
|
77 |
| - // Export the class so that new instances can be created with proper |
78 |
| - // constructor params. |
79 |
| - Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec; |
80 |
| -})(); |
| 76 | +// Export the class so that new instances can be created with proper |
| 77 | +// constructor params. |
| 78 | +Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec; |
0 commit comments