|
| 1 | +import {BaseException, global} from 'angular2/src/facade/lang'; |
| 2 | +import {ListWrapper} from 'angular2/src/facade/collection'; |
| 3 | + |
| 4 | +var _scheduler; |
| 5 | +var _microtasks:List<Function> = []; |
| 6 | +var _pendingPeriodicTimers: List<number> = []; |
| 7 | +var _pendingTimers: List<number> = []; |
| 8 | +var _error = null; |
| 9 | + |
| 10 | +/** |
| 11 | + * Wraps a function to be executed in the fakeAsync zone: |
| 12 | + * - microtasks are manually executed by calling `flushMicrotasks()`, |
| 13 | + * - timers are synchronous, `tick()` simulates the asynchronous passage of time. |
| 14 | + * |
| 15 | + * If there are any pending timers at the end of the function, an exception will be thrown. |
| 16 | + * |
| 17 | + * @param fn |
| 18 | + * @returns {Function} The function wrapped to be executed in the fakeAsync zone |
| 19 | + */ |
| 20 | +export function fakeAsync(fn: Function): Function { |
| 21 | + // TODO(vicb) re-enable once the jasmine patch from zone.js is applied |
| 22 | + //if (global.zone._inFakeAsyncZone) { |
| 23 | + // throw new Error('fakeAsync() calls can not be nested'); |
| 24 | + //} |
| 25 | + |
| 26 | + var fakeAsyncZone = global.zone.fork({ |
| 27 | + setTimeout: _setTimeout, |
| 28 | + clearTimeout: _clearTimeout, |
| 29 | + setInterval: _setInterval, |
| 30 | + clearInterval: _clearInterval, |
| 31 | + scheduleMicrotask: _scheduleMicrotask, |
| 32 | + _inFakeAsyncZone: true |
| 33 | + }); |
| 34 | + |
| 35 | + return function(...args) { |
| 36 | + _scheduler = global.jasmine.DelayedFunctionScheduler(); |
| 37 | + ListWrapper.clear(_microtasks); |
| 38 | + ListWrapper.clear(_pendingPeriodicTimers); |
| 39 | + ListWrapper.clear(_pendingTimers); |
| 40 | + |
| 41 | + var res = fakeAsyncZone.run(() => { |
| 42 | + var res = fn(...args); |
| 43 | + }); |
| 44 | + |
| 45 | + if (_pendingPeriodicTimers.length > 0) { |
| 46 | + throw new BaseException(`${_pendingPeriodicTimers.length} periodic timer(s) still in the queue.`); |
| 47 | + } |
| 48 | + |
| 49 | + if (_pendingTimers.length > 0) { |
| 50 | + throw new BaseException(`${_pendingTimers.length} timer(s) still in the queue.`); |
| 51 | + } |
| 52 | + |
| 53 | + _scheduler = null; |
| 54 | + ListWrapper.clear(_microtasks); |
| 55 | + |
| 56 | + return res; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Simulates the asynchronous passage of time for the timers in the fakeAsync zone. |
| 62 | + * |
| 63 | + * The microtasks queue is drained at the very start of this function and after any timer callback has been executed. |
| 64 | + * |
| 65 | + * @param {number} millis Number of millisecond, defaults to 0 |
| 66 | + */ |
| 67 | +export function tick(millis: number = 0): void { |
| 68 | + _assertInFakeAsyncZone(); |
| 69 | + flushMicrotasks(); |
| 70 | + _scheduler.tick(millis); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Flush any pending microtasks. |
| 75 | + */ |
| 76 | +export function flushMicrotasks(): void { |
| 77 | + _assertInFakeAsyncZone(); |
| 78 | + while (_microtasks.length > 0) { |
| 79 | + var microtask = ListWrapper.removeAt(_microtasks, 0); |
| 80 | + microtask(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function _setTimeout(fn: Function, delay: number, ...args): number { |
| 85 | + var cb = _fnAndFlush(fn); |
| 86 | + var id = _scheduler.scheduleFunction(cb, delay, args); |
| 87 | + ListWrapper.push(_pendingTimers, id); |
| 88 | + _scheduler.scheduleFunction(_dequeueTimer(id), delay); |
| 89 | + return id; |
| 90 | +} |
| 91 | + |
| 92 | +function _clearTimeout(id: number) { |
| 93 | + _dequeueTimer(id); |
| 94 | + return _scheduler.removeFunctionWithId(id); |
| 95 | +} |
| 96 | + |
| 97 | +function _setInterval(fn: Function, interval: number, ...args) { |
| 98 | + var cb = _fnAndFlush(fn); |
| 99 | + var id = _scheduler.scheduleFunction(cb, interval, args, true); |
| 100 | + _pendingPeriodicTimers.push(id); |
| 101 | + return id; |
| 102 | +} |
| 103 | + |
| 104 | +function _clearInterval(id: number) { |
| 105 | + ListWrapper.remove(_pendingPeriodicTimers, id); |
| 106 | + return _scheduler.removeFunctionWithId(id); |
| 107 | +} |
| 108 | + |
| 109 | +function _fnAndFlush(fn: Function): void { |
| 110 | + return () => { |
| 111 | + fn.apply(global, arguments); |
| 112 | + flushMicrotasks(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function _scheduleMicrotask(microtask: Function): void { |
| 117 | + ListWrapper.push(_microtasks, microtask); |
| 118 | +} |
| 119 | + |
| 120 | +function _dequeueTimer(id: number): Function { |
| 121 | + return function() { |
| 122 | + ListWrapper.remove(_pendingTimers, id); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function _assertInFakeAsyncZone(): void { |
| 127 | + if (!global.zone._inFakeAsyncZone) { |
| 128 | + throw new Error('The code should be running in the fakeAsync zone to call this function'); |
| 129 | + } |
| 130 | +} |
| 131 | + |
0 commit comments