Observable.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { SafeSubscriber, Subscriber } from './Subscriber';
  2. import { isSubscription } from './Subscription';
  3. import { observable as Symbol_observable } from './symbol/observable';
  4. import { pipeFromArray } from './util/pipe';
  5. import { config } from './config';
  6. import { isFunction } from './util/isFunction';
  7. import { errorContext } from './util/errorContext';
  8. export class Observable {
  9. constructor(subscribe) {
  10. if (subscribe) {
  11. this._subscribe = subscribe;
  12. }
  13. }
  14. lift(operator) {
  15. const observable = new Observable();
  16. observable.source = this;
  17. observable.operator = operator;
  18. return observable;
  19. }
  20. subscribe(observerOrNext, error, complete) {
  21. const subscriber = isSubscriber(observerOrNext) ? observerOrNext : new SafeSubscriber(observerOrNext, error, complete);
  22. errorContext(() => {
  23. const { operator, source } = this;
  24. subscriber.add(operator
  25. ?
  26. operator.call(subscriber, source)
  27. : source
  28. ?
  29. this._subscribe(subscriber)
  30. :
  31. this._trySubscribe(subscriber));
  32. });
  33. return subscriber;
  34. }
  35. _trySubscribe(sink) {
  36. try {
  37. return this._subscribe(sink);
  38. }
  39. catch (err) {
  40. sink.error(err);
  41. }
  42. }
  43. forEach(next, promiseCtor) {
  44. promiseCtor = getPromiseCtor(promiseCtor);
  45. return new promiseCtor((resolve, reject) => {
  46. const subscriber = new SafeSubscriber({
  47. next: (value) => {
  48. try {
  49. next(value);
  50. }
  51. catch (err) {
  52. reject(err);
  53. subscriber.unsubscribe();
  54. }
  55. },
  56. error: reject,
  57. complete: resolve,
  58. });
  59. this.subscribe(subscriber);
  60. });
  61. }
  62. _subscribe(subscriber) {
  63. var _a;
  64. return (_a = this.source) === null || _a === void 0 ? void 0 : _a.subscribe(subscriber);
  65. }
  66. [Symbol_observable]() {
  67. return this;
  68. }
  69. pipe(...operations) {
  70. return pipeFromArray(operations)(this);
  71. }
  72. toPromise(promiseCtor) {
  73. promiseCtor = getPromiseCtor(promiseCtor);
  74. return new promiseCtor((resolve, reject) => {
  75. let value;
  76. this.subscribe((x) => (value = x), (err) => reject(err), () => resolve(value));
  77. });
  78. }
  79. }
  80. Observable.create = (subscribe) => {
  81. return new Observable(subscribe);
  82. };
  83. function getPromiseCtor(promiseCtor) {
  84. var _a;
  85. return (_a = promiseCtor !== null && promiseCtor !== void 0 ? promiseCtor : config.Promise) !== null && _a !== void 0 ? _a : Promise;
  86. }
  87. function isObserver(value) {
  88. return value && isFunction(value.next) && isFunction(value.error) && isFunction(value.complete);
  89. }
  90. function isSubscriber(value) {
  91. return (value && value instanceof Subscriber) || (isObserver(value) && isSubscription(value));
  92. }
  93. //# sourceMappingURL=Observable.js.map