|
1 | | -import {global, isPresent} from 'angular2/src/facade/lang'; |
| 1 | +import {global, isPresent, noop} from 'angular2/src/facade/lang'; |
2 | 2 | // We make sure promises are in a separate file so that we can use promises |
3 | 3 | // without depending on rxjs. |
4 | 4 | import {PromiseWrapper, Promise, PromiseCompleter} from 'angular2/src/facade/promise'; |
@@ -27,6 +27,8 @@ export class ObservableWrapper { |
27 | 27 | // TODO(vsavkin): when we use rxnext, try inferring the generic type from the first arg |
28 | 28 | static subscribe<T>(emitter: any, onNext: (value: T) => void, onError?: (exception: any) => void, |
29 | 29 | onComplete: () => void = () => {}): Object { |
| 30 | + onError = (typeof onError === "function") && onError || noop; |
| 31 | + onComplete = (typeof onComplete === "function") && onComplete || noop; |
30 | 32 | return emitter.subscribe({next: onNext, error: onError, complete: onComplete}); |
31 | 33 | } |
32 | 34 |
|
@@ -117,20 +119,39 @@ export class EventEmitter<T> extends Subject<T> { |
117 | 119 | next(value: any) { super.next(value); } |
118 | 120 |
|
119 | 121 | subscribe(generatorOrNext?: any, error?: any, complete?: any): any { |
| 122 | + let schedulerFn; |
| 123 | + let errorFn = (err: any) => null; |
| 124 | + let completeFn = () => null; |
| 125 | + |
120 | 126 | if (generatorOrNext && typeof generatorOrNext === 'object') { |
121 | | - let schedulerFn = this._isAsync ? |
122 | | - (value) => { setTimeout(() => generatorOrNext.next(value)); } : |
123 | | - (value) => { generatorOrNext.next(value); }; |
124 | | - return super.subscribe(schedulerFn, |
125 | | - (err) => generatorOrNext.error ? generatorOrNext.error(err) : null, |
126 | | - () => generatorOrNext.complete ? generatorOrNext.complete() : null); |
| 127 | + schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext.next(value)); } : |
| 128 | + (value) => { generatorOrNext.next(value); }; |
| 129 | + |
| 130 | + if (generatorOrNext.error) { |
| 131 | + errorFn = this._isAsync ? (err) => { setTimeout(() => generatorOrNext.error(err)); } : |
| 132 | + (err) => { generatorOrNext.error(err); }; |
| 133 | + } |
| 134 | + |
| 135 | + if (generatorOrNext.complete) { |
| 136 | + completeFn = this._isAsync ? () => { setTimeout(() => generatorOrNext.complete()); } : |
| 137 | + () => { generatorOrNext.complete(); }; |
| 138 | + } |
127 | 139 | } else { |
128 | | - let schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext(value)); } : |
129 | | - (value) => { generatorOrNext(value); }; |
130 | | - |
131 | | - return super.subscribe(schedulerFn, (err) => error ? error(err) : null, |
132 | | - () => complete ? complete() : null); |
| 140 | + schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext(value)); } : |
| 141 | + (value) => { generatorOrNext(value); }; |
| 142 | + |
| 143 | + if (error) { |
| 144 | + errorFn = |
| 145 | + this._isAsync ? (err) => { setTimeout(() => error(err)); } : (err) => { error(err); }; |
| 146 | + } |
| 147 | + |
| 148 | + if (complete) { |
| 149 | + completeFn = |
| 150 | + this._isAsync ? () => { setTimeout(() => complete()); } : () => { complete(); }; |
| 151 | + } |
133 | 152 | } |
| 153 | + |
| 154 | + return super.subscribe(schedulerFn, errorFn, completeFn); |
134 | 155 | } |
135 | 156 | } |
136 | 157 |
|
|
0 commit comments