ErrorObservable.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Observable } from '../Observable';
  2. /**
  3. * We need this JSDoc comment for affecting ESDoc.
  4. * @extends {Ignored}
  5. * @hide true
  6. */
  7. export class ErrorObservable extends Observable {
  8. constructor(error, scheduler) {
  9. super();
  10. this.error = error;
  11. this.scheduler = scheduler;
  12. }
  13. /**
  14. * Creates an Observable that emits no items to the Observer and immediately
  15. * emits an error notification.
  16. *
  17. * <span class="informal">Just emits 'error', and nothing else.
  18. * </span>
  19. *
  20. * <img src="./img/throw.png" width="100%">
  21. *
  22. * This static operator is useful for creating a simple Observable that only
  23. * emits the error notification. It can be used for composing with other
  24. * Observables, such as in a {@link mergeMap}.
  25. *
  26. * @example <caption>Emit the number 7, then emit an error.</caption>
  27. * var result = Rx.Observable.throw(new Error('oops!')).startWith(7);
  28. * result.subscribe(x => console.log(x), e => console.error(e));
  29. *
  30. * @example <caption>Map and flatten numbers to the sequence 'a', 'b', 'c', but throw an error for 13</caption>
  31. * var interval = Rx.Observable.interval(1000);
  32. * var result = interval.mergeMap(x =>
  33. * x === 13 ?
  34. * Rx.Observable.throw('Thirteens are bad') :
  35. * Rx.Observable.of('a', 'b', 'c')
  36. * );
  37. * result.subscribe(x => console.log(x), e => console.error(e));
  38. *
  39. * @see {@link create}
  40. * @see {@link empty}
  41. * @see {@link never}
  42. * @see {@link of}
  43. *
  44. * @param {any} error The particular Error to pass to the error notification.
  45. * @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling
  46. * the emission of the error notification.
  47. * @return {Observable} An error Observable: emits only the error notification
  48. * using the given error argument.
  49. * @static true
  50. * @name throw
  51. * @owner Observable
  52. */
  53. static create(error, scheduler) {
  54. return new ErrorObservable(error, scheduler);
  55. }
  56. static dispatch(arg) {
  57. const { error, subscriber } = arg;
  58. subscriber.error(error);
  59. }
  60. /** @deprecated internal use only */ _subscribe(subscriber) {
  61. const error = this.error;
  62. const scheduler = this.scheduler;
  63. subscriber.syncErrorThrowable = true;
  64. if (scheduler) {
  65. return scheduler.schedule(ErrorObservable.dispatch, 0, {
  66. error, subscriber
  67. });
  68. }
  69. else {
  70. subscriber.error(error);
  71. }
  72. }
  73. }
  74. //# sourceMappingURL=ErrorObservable.js.map