-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(throwError): functional version of throwError
- eliminates `ErrorObservable` - eliminates `_throw` - updates tests BREAKING CHANGE: Observable.throw no longer available in TypeScript without a cast
- Loading branch information
Showing
9 changed files
with
74 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Observable } from '../../internal/Observable'; | ||
import { _throw } from '../../internal/observable/throw'; | ||
import { throwError } from '../../internal/observable/throwError'; | ||
|
||
Observable.throw = _throw; | ||
(Observable as any).throw = throwError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Observable } from '../Observable'; | ||
import { IScheduler } from '../Scheduler'; | ||
import { Subscriber } from '../Subscriber'; | ||
|
||
/** | ||
* Creates an Observable that emits no items to the Observer and immediately | ||
* emits an error notification. | ||
* | ||
* <span class="informal">Just emits 'error', and nothing else. | ||
* </span> | ||
* | ||
* <img src="./img/throw.png" width="100%"> | ||
* | ||
* This static operator is useful for creating a simple Observable that only | ||
* emits the error notification. It can be used for composing with other | ||
* Observables, such as in a {@link mergeMap}. | ||
* | ||
* @example <caption>Emit the number 7, then emit an error.</caption> | ||
* var result = Rx.Observable.throw(new Error('oops!')).startWith(7); | ||
* result.subscribe(x => console.log(x), e => console.error(e)); | ||
* | ||
* @example <caption>Map and flatten numbers to the sequence 'a', 'b', 'c', but throw an error for 13</caption> | ||
* var interval = Rx.Observable.interval(1000); | ||
* var result = interval.mergeMap(x => | ||
* x === 13 ? | ||
* Rx.Observable.throw('Thirteens are bad') : | ||
* Rx.Observable.of('a', 'b', 'c') | ||
* ); | ||
* result.subscribe(x => console.log(x), e => console.error(e)); | ||
* | ||
* @see {@link create} | ||
* @see {@link empty} | ||
* @see {@link never} | ||
* @see {@link of} | ||
* | ||
* @param {any} error The particular Error to pass to the error notification. | ||
* @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling | ||
* the emission of the error notification. | ||
* @return {Observable} An error Observable: emits only the error notification | ||
* using the given error argument. | ||
* @static true | ||
* @name throw | ||
* @owner Observable | ||
*/ | ||
export function throwError(error: any, scheduler?: IScheduler): Observable<never> { | ||
if (!scheduler) { | ||
return new Observable(subscriber => subscriber.error(error)); | ||
} else { | ||
return new Observable(subscriber => scheduler.schedule(dispatch, 0, { error, subscriber })); | ||
} | ||
} | ||
|
||
interface DispatchArg { | ||
error: any; | ||
subscriber: Subscriber<any>; | ||
} | ||
|
||
function dispatch({ error, subscriber }: DispatchArg) { | ||
subscriber.error(error); | ||
} |