fix(EventEmitter): resolve onError and onComplete asynchronously

closes #4443
This commit is contained in:
Nathan Walker
2015-10-29 19:57:28 -07:00
committed by Alex Rickabaugh
parent b4de41b74e
commit 019cb41dd8
3 changed files with 79 additions and 22 deletions

View File

@ -1,4 +1,4 @@
import {global, isPresent} from 'angular2/src/facade/lang';
import {global, isPresent, noop} from 'angular2/src/facade/lang';
// We make sure promises are in a separate file so that we can use promises
// without depending on rxjs.
import {PromiseWrapper, Promise, PromiseCompleter} from 'angular2/src/facade/promise';
@ -27,6 +27,8 @@ export class ObservableWrapper {
// TODO(vsavkin): when we use rxnext, try inferring the generic type from the first arg
static subscribe<T>(emitter: any, onNext: (value: T) => void, onError?: (exception: any) => void,
onComplete: () => void = () => {}): Object {
onError = (typeof onError === "function") && onError || noop;
onComplete = (typeof onComplete === "function") && onComplete || noop;
return emitter.subscribe({next: onNext, error: onError, complete: onComplete});
}
@ -117,20 +119,39 @@ export class EventEmitter<T> extends Subject<T> {
next(value: any) { super.next(value); }
subscribe(generatorOrNext?: any, error?: any, complete?: any): any {
if (generatorOrNext && typeof generatorOrNext === 'object') {
let schedulerFn = this._isAsync ?
(value) => { setTimeout(() => generatorOrNext.next(value)); } :
(value) => { generatorOrNext.next(value); };
return super.subscribe(schedulerFn,
(err) => generatorOrNext.error ? generatorOrNext.error(err) : null,
() => generatorOrNext.complete ? generatorOrNext.complete() : null);
} else {
let schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext(value)); } :
(value) => { generatorOrNext(value); };
let schedulerFn;
let errorFn = (err: any) => null;
let completeFn = () => null;
return super.subscribe(schedulerFn, (err) => error ? error(err) : null,
() => complete ? complete() : null);
if (generatorOrNext && typeof generatorOrNext === 'object') {
schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext.next(value)); } :
(value) => { generatorOrNext.next(value); };
if (generatorOrNext.error) {
errorFn = this._isAsync ? (err) => { setTimeout(() => generatorOrNext.error(err)); } :
(err) => { generatorOrNext.error(err); };
}
if (generatorOrNext.complete) {
completeFn = this._isAsync ? () => { setTimeout(() => generatorOrNext.complete()); } :
() => { generatorOrNext.complete(); };
}
} else {
schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext(value)); } :
(value) => { generatorOrNext(value); };
if (error) {
errorFn =
this._isAsync ? (err) => { setTimeout(() => error(err)); } : (err) => { error(err); };
}
if (complete) {
completeFn =
this._isAsync ? () => { setTimeout(() => complete()); } : () => { complete(); };
}
}
return super.subscribe(schedulerFn, errorFn, completeFn);
}
}

View File

@ -126,6 +126,8 @@ export function isDate(obj): boolean {
return obj instanceof Date && !isNaN(obj.valueOf());
}
export function noop() {}
export function stringify(token): string {
if (typeof token === 'string') {
return token;