refactor(async): extract timer related functions into a TimerWrapper

This commit is contained in:
Victor Berchet 2015-05-19 16:47:30 +02:00
parent 62b1a08f06
commit 6ec5d5daaf
8 changed files with 51 additions and 51 deletions

View File

@ -29,7 +29,12 @@ class PromiseWrapper {
static CompleterWrapper completer() => new CompleterWrapper(new Completer()); 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) static Timer setTimeout(fn(), int millis)
=> new Timer(new Duration(milliseconds: millis), fn); => new Timer(new Duration(milliseconds: millis), fn);
static void clearTimeout(Timer timer) { static void clearTimeout(Timer timer) {
@ -43,10 +48,6 @@ class PromiseWrapper {
static void clearInterval(Timer timer) { static void clearInterval(Timer timer) {
timer.cancel(); timer.cancel();
} }
static bool isPromise(maybePromise) {
return maybePromise is Future;
}
} }
class ObservableWrapper { class ObservableWrapper {

View File

@ -39,18 +39,17 @@ export class PromiseWrapper {
return {promise: p, resolve: resolve, reject: reject}; 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 setTimeout(fn: Function, millis: int): int { return global.setTimeout(fn, millis); }
static clearTimeout(id: int): void { global.clearTimeout(id); } static clearTimeout(id: int): void { global.clearTimeout(id); }
static setInterval(fn: Function, millis: int): int { return global.setInterval(fn, millis); } static setInterval(fn: Function, millis: int): int { return global.setInterval(fn, millis); }
static clearInterval(id: int): void { global.clearInterval(id); } static clearInterval(id: int): void { global.clearInterval(id); }
static isPromise(maybePromise): boolean { return maybePromise instanceof Promise; }
} }
export class ObservableWrapper { export class ObservableWrapper {
static subscribe(emitter: Observable, onNext, onThrow = null, onReturn = null): Object { static subscribe(emitter: Observable, onNext, onThrow = null, onReturn = null): Object {
return emitter.observer({next: onNext, throw: onThrow, return: onReturn}); return emitter.observer({next: onNext, throw: onThrow, return: onReturn});

View File

@ -5,7 +5,7 @@ import {IMPLEMENTS} from 'angular2/src/facade/lang';
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe'; import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
import {ObservablePipe} from 'angular2/src/change_detection/pipes/observable_pipe'; import {ObservablePipe} from 'angular2/src/change_detection/pipes/observable_pipe';
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref'; 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() { export function main() {
describe("ObservablePipe", () => { describe("ObservablePipe", () => {
@ -41,7 +41,7 @@ export function main() {
ObservableWrapper.callNext(emitter, message); ObservableWrapper.callNext(emitter, message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message)); expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
async.done(); async.done();
}, 0) }, 0)
@ -52,7 +52,7 @@ export function main() {
pipe.transform(emitter); pipe.transform(emitter);
ObservableWrapper.callNext(emitter, message); ObservableWrapper.callNext(emitter, message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
pipe.transform(emitter); pipe.transform(emitter);
expect(pipe.transform(emitter)).toBe(message); expect(pipe.transform(emitter)).toBe(message);
async.done(); async.done();
@ -69,7 +69,7 @@ export function main() {
// this should not affect the pipe // this should not affect the pipe
ObservableWrapper.callNext(emitter, message); ObservableWrapper.callNext(emitter, message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(pipe.transform(newEmitter)).toBe(null); expect(pipe.transform(newEmitter)).toBe(null);
async.done(); async.done();
}, 0) }, 0)
@ -80,7 +80,7 @@ export function main() {
pipe.transform(emitter); pipe.transform(emitter);
ObservableWrapper.callNext(emitter, message); ObservableWrapper.callNext(emitter, message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(ref.spy('requestCheck')).toHaveBeenCalled(); expect(ref.spy('requestCheck')).toHaveBeenCalled();
async.done(); async.done();
}, 0) }, 0)
@ -98,7 +98,7 @@ export function main() {
ObservableWrapper.callNext(emitter, message); ObservableWrapper.callNext(emitter, message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(pipe.transform(emitter)).toBe(null); expect(pipe.transform(emitter)).toBe(null);
async.done(); async.done();
}, 0) }, 0)

View File

@ -4,7 +4,7 @@ import {IMPLEMENTS} from 'angular2/src/facade/lang';
import {PromisePipe} from 'angular2/src/change_detection/pipes/promise_pipe'; import {PromisePipe} from 'angular2/src/change_detection/pipes/promise_pipe';
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe'; import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref'; 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() { export function main() {
describe("PromisePipe", () => { describe("PromisePipe", () => {
@ -40,7 +40,7 @@ export function main() {
completer.resolve(message); completer.resolve(message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message)); expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
async.done(); async.done();
}, 0) }, 0)
@ -51,7 +51,7 @@ export function main() {
pipe.transform(completer.promise); pipe.transform(completer.promise);
completer.resolve(message); completer.resolve(message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
pipe.transform(completer.promise); pipe.transform(completer.promise);
expect(pipe.transform(completer.promise)).toBe(message); expect(pipe.transform(completer.promise)).toBe(message);
async.done(); async.done();
@ -68,7 +68,7 @@ export function main() {
// this should not affect the pipe, so it should return WrappedValue // this should not affect the pipe, so it should return WrappedValue
completer.resolve(message); completer.resolve(message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(pipe.transform(newCompleter.promise)).toBe(null); expect(pipe.transform(newCompleter.promise)).toBe(null);
async.done(); async.done();
}, 0) }, 0)
@ -79,7 +79,7 @@ export function main() {
pipe.transform(completer.promise); pipe.transform(completer.promise);
completer.resolve(message); completer.resolve(message);
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(ref.spy('requestCheck')).toHaveBeenCalled(); expect(ref.spy('requestCheck')).toHaveBeenCalled();
async.done(); async.done();
}, 0) }, 0)

View File

@ -13,7 +13,7 @@ import {
isInInnerZone isInInnerZone
} from 'angular2/test_lib'; } 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 {ListWrapper} from 'angular2/src/facade/collection';
import {BaseException} from 'angular2/src/facade/lang'; 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) // Schedules a macrotask (using a timer)
function macroTask(fn: Function): void { 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()) // Schedules a microtasks (using a resolved promise .then())
@ -71,8 +71,8 @@ export function main() {
var c = PromiseWrapper.completer(); var c = PromiseWrapper.completer();
_zone.run(() => { _zone.run(() => {
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
c.resolve(null); c.resolve(null);
throw new BaseException('ccc'); throw new BaseException('ccc');
}, 0); }, 0);
@ -124,8 +124,8 @@ export function main() {
var c = PromiseWrapper.completer(); var c = PromiseWrapper.completer();
_zone.run(() => { _zone.run(() => {
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
c.resolve(null); c.resolve(null);
throw new BaseException('ccc'); throw new BaseException('ccc');
}, 0); }, 0);

View File

@ -14,7 +14,7 @@ import {
tick, tick,
xit xit
} from 'angular2/test_lib'; } 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 {BaseException, global} from 'angular2/src/facade/lang';
import {Parser} from 'angular2/change_detection'; import {Parser} from 'angular2/change_detection';
@ -120,7 +120,7 @@ export function main() {
describe('timers', () => { describe('timers', () => {
it('should run queued zero duration timer on zero tick', fakeAsync(() => { it('should run queued zero duration timer on zero tick', fakeAsync(() => {
var ran = false; var ran = false;
PromiseWrapper.setTimeout(() => { ran = true }, 0); TimerWrapper.setTimeout(() => { ran = true }, 0);
expect(ran).toEqual(false); expect(ran).toEqual(false);
@ -131,7 +131,7 @@ export function main() {
it('should run queued timer after sufficient clock ticks', fakeAsync(() => { it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
var ran = false; var ran = false;
PromiseWrapper.setTimeout(() => { ran = true; }, 10); TimerWrapper.setTimeout(() => { ran = true; }, 10);
tick(6); tick(6);
expect(ran).toEqual(false); expect(ran).toEqual(false);
@ -142,7 +142,7 @@ export function main() {
it('should run queued timer only once', fakeAsync(() => { it('should run queued timer only once', fakeAsync(() => {
var cycles = 0; var cycles = 0;
PromiseWrapper.setTimeout(() => { cycles++; }, 10); TimerWrapper.setTimeout(() => { cycles++; }, 10);
tick(10); tick(10);
expect(cycles).toEqual(1); expect(cycles).toEqual(1);
@ -156,8 +156,8 @@ export function main() {
it('should not run cancelled timer', fakeAsync(() => { it('should not run cancelled timer', fakeAsync(() => {
var ran = false; var ran = false;
var id = PromiseWrapper.setTimeout(() => { ran = true; }, 10); var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
PromiseWrapper.clearTimeout(id); TimerWrapper.clearTimeout(id);
tick(10); tick(10);
expect(ran).toEqual(false); expect(ran).toEqual(false);
@ -168,7 +168,7 @@ export function main() {
if (IS_DARTIUM) return; if (IS_DARTIUM) return;
expect(() => { expect(() => {
fakeAsync(() => { fakeAsync(() => {
PromiseWrapper.setTimeout(() => { }, 10); TimerWrapper.setTimeout(() => { }, 10);
})(); })();
}).toThrowError('1 timer(s) still in the queue.'); }).toThrowError('1 timer(s) still in the queue.');
}); });
@ -178,14 +178,14 @@ export function main() {
if (IS_DARTIUM) return; if (IS_DARTIUM) return;
expect(() => { expect(() => {
fakeAsync(() => { fakeAsync(() => {
PromiseWrapper.setInterval(() => { }, 10); TimerWrapper.setInterval(() => { }, 10);
})(); })();
}).toThrowError('1 periodic timer(s) still in the queue.'); }).toThrowError('1 periodic timer(s) still in the queue.');
}); });
it('should run periodic timers', fakeAsync(() => { it('should run periodic timers', fakeAsync(() => {
var cycles = 0; var cycles = 0;
var id = PromiseWrapper.setInterval(() => { cycles++; }, 10); var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
tick(10); tick(10);
expect(cycles).toEqual(1); expect(cycles).toEqual(1);
@ -196,13 +196,13 @@ export function main() {
tick(10); tick(10);
expect(cycles).toEqual(3); expect(cycles).toEqual(3);
PromiseWrapper.clearInterval(id); TimerWrapper.clearInterval(id);
})); }));
it('should not run cancelled periodic timer', fakeAsync(() => { it('should not run cancelled periodic timer', fakeAsync(() => {
var ran = false; var ran = false;
var id = PromiseWrapper.setInterval(() => { ran = true; }, 10); var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
PromiseWrapper.clearInterval(id); TimerWrapper.clearInterval(id);
tick(10); tick(10);
expect(ran).toEqual(false); expect(ran).toEqual(false);
@ -218,9 +218,9 @@ export function main() {
var cycles = 0; var cycles = 0;
var id; var id;
id = PromiseWrapper.setInterval(() => { id = TimerWrapper.setInterval(() => {
cycles++; cycles++;
PromiseWrapper.clearInterval(id); TimerWrapper.clearInterval(id);
}, 10); }, 10);
tick(10); tick(10);
@ -235,16 +235,16 @@ export function main() {
PromiseWrapper.resolve(null).then((_) => log.add('microtask')); 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(''); expect(log.result()).toEqual('');
tick(10); tick(10);
expect(log.result()).toEqual('microtask; timer; periodic timer'); 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(() => { 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.resolve(null).then((_) => log.add('microtask'));
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
log.add('timer'); log.add('timer');
PromiseWrapper.resolve(null).then((_) => log.add('t microtask')); PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
}, 9); }, 9);
var id = PromiseWrapper.setInterval(() => { var id = TimerWrapper.setInterval(() => {
log.add('periodic timer'); log.add('periodic timer');
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask')); PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
}, 10); }, 10);
@ -268,7 +268,7 @@ export function main() {
tick(10); tick(10);
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask'); expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
PromiseWrapper.clearInterval(id); TimerWrapper.clearInterval(id);
})); }));
}); });

View File

@ -1,6 +1,6 @@
import {int, isPresent} from 'angular2/src/facade/lang'; import {int, isPresent} from 'angular2/src/facade/lang';
import {getIntParameter, bindAction} from 'angular2/src/test_lib/benchmark_util'; 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 {ListWrapper} from 'angular2/src/facade/collection';
import {ScrollAreaComponent} from './scroll_area'; import {ScrollAreaComponent} from './scroll_area';
import {NgIf, NgFor} from 'angular2/directives'; import {NgIf, NgFor} from 'angular2/directives';
@ -57,7 +57,7 @@ export class App {
var n:int = this.iterationCount; var n:int = this.iterationCount;
var scheduleScroll; var scheduleScroll;
scheduleScroll = () => { scheduleScroll = () => {
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
scrollDiv.scrollTop += this.scrollIncrement; scrollDiv.scrollTop += this.scrollIncrement;
n--; n--;
if (n > 0) { if (n > 0) {
@ -77,7 +77,7 @@ export class App {
// Nothing to do, the marker is already there // Nothing to do, the marker is already there
return; return;
} }
PromiseWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
var finishedDiv = DOM.createElement('div'); var finishedDiv = DOM.createElement('div');
finishedDiv.id = 'done'; finishedDiv.id = 'done';
DOM.setInnerHTML(finishedDiv, 'Finished'); DOM.setInnerHTML(finishedDiv, 'Finished');

View File

@ -1,4 +1,4 @@
import { PromiseWrapper, Promise } from 'angular2/src/facade/async'; import { PromiseWrapper, Promise, TimerWrapper } from 'angular2/src/facade/async';
import { import {
isPresent, isBlank, int, BaseException, StringWrapper, Math, RegExpWrapper, NumberWrapper isPresent, isBlank, int, BaseException, StringWrapper, Math, RegExpWrapper, NumberWrapper
} from 'angular2/src/facade/lang'; } from 'angular2/src/facade/lang';
@ -248,5 +248,5 @@ var _BINDINGS = [
new PerflogMetric(driverExtension, setTimeout, microMetrics, forceGc), new PerflogMetric(driverExtension, setTimeout, microMetrics, forceGc),
[WebDriverExtension, _SET_TIMEOUT, Options.MICRO_METRICS, Options.FORCE_GC] [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) )
]; ];