1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
|
9 | var ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
10 | var _fakeAsyncTestZoneSpec = null;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function resetFakeAsyncZone() {
|
18 | _fakeAsyncTestZoneSpec = null;
|
19 | ProxyZoneSpec.assertPresent().resetDelegate();
|
20 | }
|
21 | var _inFakeAsyncCall = false;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export function fakeAsync(fn) {
|
41 | return function () {
|
42 | var args = [];
|
43 | for (var _i = 0; _i < arguments.length; _i++) {
|
44 | args[_i - 0] = arguments[_i];
|
45 | }
|
46 | var proxyZoneSpec = ProxyZoneSpec.assertPresent();
|
47 | if (_inFakeAsyncCall) {
|
48 | throw new Error('fakeAsync() calls can not be nested');
|
49 | }
|
50 | _inFakeAsyncCall = true;
|
51 | try {
|
52 | if (!_fakeAsyncTestZoneSpec) {
|
53 | if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
|
54 | throw new Error('fakeAsync() calls can not be nested');
|
55 | }
|
56 | _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
|
57 | }
|
58 | var res = void 0;
|
59 | var lastProxyZoneSpec = proxyZoneSpec.getDelegate();
|
60 | proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
|
61 | try {
|
62 | res = fn.apply(void 0, args);
|
63 | flushMicrotasks();
|
64 | }
|
65 | finally {
|
66 | proxyZoneSpec.setDelegate(lastProxyZoneSpec);
|
67 | }
|
68 | if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
69 | throw new Error((_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length + " ") +
|
70 | "periodic timer(s) still in the queue.");
|
71 | }
|
72 | if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
73 | throw new Error(_fakeAsyncTestZoneSpec.pendingTimers.length + " timer(s) still in the queue.");
|
74 | }
|
75 | return res;
|
76 | }
|
77 | finally {
|
78 | _inFakeAsyncCall = false;
|
79 | resetFakeAsyncZone();
|
80 | }
|
81 | };
|
82 | }
|
83 | function _getFakeAsyncZoneSpec() {
|
84 | if (_fakeAsyncTestZoneSpec == null) {
|
85 | throw new Error('The code should be running in the fakeAsync zone to call this function');
|
86 | }
|
87 | return _fakeAsyncTestZoneSpec;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | export function tick(millis) {
|
102 | if (millis === void 0) { millis = 0; }
|
103 | _getFakeAsyncZoneSpec().tick(millis);
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export function discardPeriodicTasks() {
|
111 | var zoneSpec = _getFakeAsyncZoneSpec();
|
112 | var pendingTimers = zoneSpec.pendingPeriodicTimers;
|
113 | zoneSpec.pendingPeriodicTimers.length = 0;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | export function flushMicrotasks() {
|
121 | _getFakeAsyncZoneSpec().flushMicrotasks();
|
122 | }
|
123 |
|
\ | No newline at end of file |