refactor(async): extract timer related functions into a TimerWrapper
This commit is contained in:
parent
62b1a08f06
commit
6ec5d5daaf
|
@ -29,7 +29,12 @@ class PromiseWrapper {
|
|||
|
||||
static CompleterWrapper completer() => new CompleterWrapper(new Completer());
|
||||
|
||||
// TODO(vic): create a TimerWrapper
|
||||
static bool isPromise(maybePromise) {
|
||||
return maybePromise is Future;
|
||||
}
|
||||
}
|
||||
|
||||
class TimerWrapper {
|
||||
static Timer setTimeout(fn(), int millis)
|
||||
=> new Timer(new Duration(milliseconds: millis), fn);
|
||||
static void clearTimeout(Timer timer) {
|
||||
|
@ -43,10 +48,6 @@ class PromiseWrapper {
|
|||
static void clearInterval(Timer timer) {
|
||||
timer.cancel();
|
||||
}
|
||||
|
||||
static bool isPromise(maybePromise) {
|
||||
return maybePromise is Future;
|
||||
}
|
||||
}
|
||||
|
||||
class ObservableWrapper {
|
||||
|
|
|
@ -39,18 +39,17 @@ export class PromiseWrapper {
|
|||
|
||||
return {promise: p, resolve: resolve, reject: reject};
|
||||
}
|
||||
static isPromise(maybePromise): boolean { return maybePromise instanceof Promise; }
|
||||
}
|
||||
|
||||
// TODO(vicb): create a TimerWrapper
|
||||
export class TimerWrapper {
|
||||
static setTimeout(fn: Function, millis: int): int { return global.setTimeout(fn, millis); }
|
||||
static clearTimeout(id: int): void { global.clearTimeout(id); }
|
||||
|
||||
static setInterval(fn: Function, millis: int): int { return global.setInterval(fn, millis); }
|
||||
static clearInterval(id: int): void { global.clearInterval(id); }
|
||||
|
||||
static isPromise(maybePromise): boolean { return maybePromise instanceof Promise; }
|
||||
}
|
||||
|
||||
|
||||
export class ObservableWrapper {
|
||||
static subscribe(emitter: Observable, onNext, onThrow = null, onReturn = null): Object {
|
||||
return emitter.observer({next: onNext, throw: onThrow, return: onReturn});
|
||||
|
|
|
@ -5,7 +5,7 @@ import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
|||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ObservablePipe} from 'angular2/src/change_detection/pipes/observable_pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {EventEmitter, Observable, ObservableWrapper, PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {EventEmitter, Observable, ObservableWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
export function main() {
|
||||
describe("ObservablePipe", () => {
|
||||
|
@ -41,7 +41,7 @@ export function main() {
|
|||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, 0)
|
||||
|
@ -52,7 +52,7 @@ export function main() {
|
|||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(emitter);
|
||||
expect(pipe.transform(emitter)).toBe(message);
|
||||
async.done();
|
||||
|
@ -69,7 +69,7 @@ export function main() {
|
|||
// this should not affect the pipe
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newEmitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
|
@ -80,7 +80,7 @@ export function main() {
|
|||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, 0)
|
||||
|
@ -98,7 +98,7 @@ export function main() {
|
|||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
|
|
|
@ -4,7 +4,7 @@ import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
|||
import {PromisePipe} from 'angular2/src/change_detection/pipes/promise_pipe';
|
||||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
export function main() {
|
||||
describe("PromisePipe", () => {
|
||||
|
@ -40,7 +40,7 @@ export function main() {
|
|||
|
||||
completer.resolve(message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, 0)
|
||||
|
@ -51,7 +51,7 @@ export function main() {
|
|||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(completer.promise);
|
||||
expect(pipe.transform(completer.promise)).toBe(message);
|
||||
async.done();
|
||||
|
@ -68,7 +68,7 @@ export function main() {
|
|||
// this should not affect the pipe, so it should return WrappedValue
|
||||
completer.resolve(message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
|
@ -79,7 +79,7 @@ export function main() {
|
|||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, 0)
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
isInInnerZone
|
||||
} from 'angular2/test_lib';
|
||||
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {BaseException} from 'angular2/src/facade/lang';
|
||||
|
||||
|
@ -21,7 +21,7 @@ import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
|||
|
||||
// Schedules a macrotask (using a timer)
|
||||
function macroTask(fn: Function): void {
|
||||
_zone.runOutsideAngular(() => PromiseWrapper.setTimeout(fn, 1));
|
||||
_zone.runOutsideAngular(() => TimerWrapper.setTimeout(fn, 1));
|
||||
}
|
||||
|
||||
// Schedules a microtasks (using a resolved promise .then())
|
||||
|
@ -71,8 +71,8 @@ export function main() {
|
|||
var c = PromiseWrapper.completer();
|
||||
|
||||
_zone.run(() => {
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
c.resolve(null);
|
||||
throw new BaseException('ccc');
|
||||
}, 0);
|
||||
|
@ -124,8 +124,8 @@ export function main() {
|
|||
var c = PromiseWrapper.completer();
|
||||
|
||||
_zone.run(() => {
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
c.resolve(null);
|
||||
throw new BaseException('ccc');
|
||||
}, 0);
|
||||
|
|
|
@ -14,7 +14,7 @@ import {
|
|||
tick,
|
||||
xit
|
||||
} from 'angular2/test_lib';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {TimerWrapper, PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {BaseException, global} from 'angular2/src/facade/lang';
|
||||
import {Parser} from 'angular2/change_detection';
|
||||
|
||||
|
@ -120,7 +120,7 @@ export function main() {
|
|||
describe('timers', () => {
|
||||
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
|
||||
var ran = false;
|
||||
PromiseWrapper.setTimeout(() => { ran = true }, 0);
|
||||
TimerWrapper.setTimeout(() => { ran = true }, 0);
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
|
@ -131,7 +131,7 @@ export function main() {
|
|||
|
||||
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
|
||||
var ran = false;
|
||||
PromiseWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
TimerWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
|
||||
tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
|
@ -142,7 +142,7 @@ export function main() {
|
|||
|
||||
it('should run queued timer only once', fakeAsync(() => {
|
||||
var cycles = 0;
|
||||
PromiseWrapper.setTimeout(() => { cycles++; }, 10);
|
||||
TimerWrapper.setTimeout(() => { cycles++; }, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
@ -156,8 +156,8 @@ export function main() {
|
|||
|
||||
it('should not run cancelled timer', fakeAsync(() => {
|
||||
var ran = false;
|
||||
var id = PromiseWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
PromiseWrapper.clearTimeout(id);
|
||||
var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
TimerWrapper.clearTimeout(id);
|
||||
|
||||
tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
|
@ -168,7 +168,7 @@ export function main() {
|
|||
if (IS_DARTIUM) return;
|
||||
expect(() => {
|
||||
fakeAsync(() => {
|
||||
PromiseWrapper.setTimeout(() => { }, 10);
|
||||
TimerWrapper.setTimeout(() => { }, 10);
|
||||
})();
|
||||
}).toThrowError('1 timer(s) still in the queue.');
|
||||
});
|
||||
|
@ -178,14 +178,14 @@ export function main() {
|
|||
if (IS_DARTIUM) return;
|
||||
expect(() => {
|
||||
fakeAsync(() => {
|
||||
PromiseWrapper.setInterval(() => { }, 10);
|
||||
TimerWrapper.setInterval(() => { }, 10);
|
||||
})();
|
||||
}).toThrowError('1 periodic timer(s) still in the queue.');
|
||||
});
|
||||
|
||||
it('should run periodic timers', fakeAsync(() => {
|
||||
var cycles = 0;
|
||||
var id = PromiseWrapper.setInterval(() => { cycles++; }, 10);
|
||||
var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
@ -196,13 +196,13 @@ export function main() {
|
|||
tick(10);
|
||||
expect(cycles).toEqual(3);
|
||||
|
||||
PromiseWrapper.clearInterval(id);
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
|
||||
it('should not run cancelled periodic timer', fakeAsync(() => {
|
||||
var ran = false;
|
||||
var id = PromiseWrapper.setInterval(() => { ran = true; }, 10);
|
||||
PromiseWrapper.clearInterval(id);
|
||||
var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
|
||||
TimerWrapper.clearInterval(id);
|
||||
|
||||
tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
|
@ -218,9 +218,9 @@ export function main() {
|
|||
var cycles = 0;
|
||||
var id;
|
||||
|
||||
id = PromiseWrapper.setInterval(() => {
|
||||
id = TimerWrapper.setInterval(() => {
|
||||
cycles++;
|
||||
PromiseWrapper.clearInterval(id);
|
||||
TimerWrapper.clearInterval(id);
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
|
@ -235,16 +235,16 @@ export function main() {
|
|||
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
|
||||
|
||||
PromiseWrapper.setTimeout(() => log.add('timer'), 9);
|
||||
TimerWrapper.setTimeout(() => log.add('timer'), 9);
|
||||
|
||||
var id = PromiseWrapper.setInterval(() => log.add('periodic timer'), 10);
|
||||
var id = TimerWrapper.setInterval(() => log.add('periodic timer'), 10);
|
||||
|
||||
expect(log.result()).toEqual('');
|
||||
|
||||
tick(10);
|
||||
expect(log.result()).toEqual('microtask; timer; periodic timer');
|
||||
|
||||
PromiseWrapper.clearInterval(id);
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
|
||||
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
|
||||
|
@ -252,12 +252,12 @@ export function main() {
|
|||
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
|
||||
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
log.add('timer');
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
|
||||
}, 9);
|
||||
|
||||
var id = PromiseWrapper.setInterval(() => {
|
||||
var id = TimerWrapper.setInterval(() => {
|
||||
log.add('periodic timer');
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
|
||||
}, 10);
|
||||
|
@ -268,7 +268,7 @@ export function main() {
|
|||
tick(10);
|
||||
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
|
||||
|
||||
PromiseWrapper.clearInterval(id);
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
});
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {int, isPresent} from 'angular2/src/facade/lang';
|
||||
import {getIntParameter, bindAction} from 'angular2/src/test_lib/benchmark_util';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {TimerWrapper} from 'angular2/src/facade/async';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {ScrollAreaComponent} from './scroll_area';
|
||||
import {NgIf, NgFor} from 'angular2/directives';
|
||||
|
@ -57,7 +57,7 @@ export class App {
|
|||
var n:int = this.iterationCount;
|
||||
var scheduleScroll;
|
||||
scheduleScroll = () => {
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
scrollDiv.scrollTop += this.scrollIncrement;
|
||||
n--;
|
||||
if (n > 0) {
|
||||
|
@ -77,7 +77,7 @@ export class App {
|
|||
// Nothing to do, the marker is already there
|
||||
return;
|
||||
}
|
||||
PromiseWrapper.setTimeout(() => {
|
||||
TimerWrapper.setTimeout(() => {
|
||||
var finishedDiv = DOM.createElement('div');
|
||||
finishedDiv.id = 'done';
|
||||
DOM.setInnerHTML(finishedDiv, 'Finished');
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { PromiseWrapper, Promise } from 'angular2/src/facade/async';
|
||||
import { PromiseWrapper, Promise, TimerWrapper } from 'angular2/src/facade/async';
|
||||
import {
|
||||
isPresent, isBlank, int, BaseException, StringWrapper, Math, RegExpWrapper, NumberWrapper
|
||||
} from 'angular2/src/facade/lang';
|
||||
|
@ -248,5 +248,5 @@ var _BINDINGS = [
|
|||
new PerflogMetric(driverExtension, setTimeout, microMetrics, forceGc),
|
||||
[WebDriverExtension, _SET_TIMEOUT, Options.MICRO_METRICS, Options.FORCE_GC]
|
||||
),
|
||||
bind(_SET_TIMEOUT).toValue( (fn, millis) => PromiseWrapper.setTimeout(fn, millis) )
|
||||
bind(_SET_TIMEOUT).toValue( (fn, millis) => TimerWrapper.setTimeout(fn, millis) )
|
||||
];
|
||||
|
|
Loading…
Reference in New Issue