Observable.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. var Observable = (function () {
  9. function Observable(subscribe) {
  10. if (subscribe) {
  11. this._subscribe = subscribe;
  12. }
  13. }
  14. Observable.prototype.lift = function (operator) {
  15. var observable = new Observable();
  16. observable.source = this;
  17. observable.operator = operator;
  18. return observable;
  19. };
  20. Observable.prototype.subscribe = function (observerOrNext, error, complete) {
  21. var _this = this;
  22. var subscriber = isSubscriber(observerOrNext) ? observerOrNext : new SafeSubscriber(observerOrNext, error, complete);
  23. errorContext(function () {
  24. var _a = _this, operator = _a.operator, source = _a.source;
  25. subscriber.add(operator
  26. ?
  27. operator.call(subscriber, source)
  28. : source
  29. ?
  30. _this._subscribe(subscriber)
  31. :
  32. _this._trySubscribe(subscriber));
  33. });
  34. return subscriber;
  35. };
  36. Observable.prototype._trySubscribe = function (sink) {
  37. try {
  38. return this._subscribe(sink);
  39. }
  40. catch (err) {
  41. sink.error(err);
  42. }
  43. };
  44. Observable.prototype.forEach = function (next, promiseCtor) {
  45. var _this = this;
  46. promiseCtor = getPromiseCtor(promiseCtor);
  47. return new promiseCtor(function (resolve, reject) {
  48. var subscriber = new SafeSubscriber({
  49. next: function (value) {
  50. try {
  51. next(value);
  52. }
  53. catch (err) {
  54. reject(err);
  55. subscriber.unsubscribe();
  56. }
  57. },
  58. error: reject,
  59. complete: resolve,
  60. });
  61. _this.subscribe(subscriber);
  62. });
  63. };
  64. Observable.prototype._subscribe = function (subscriber) {
  65. var _a;
  66. return (_a = this.source) === null || _a === void 0 ? void 0 : _a.subscribe(subscriber);
  67. };
  68. Observable.prototype[Symbol_observable] = function () {
  69. return this;
  70. };
  71. Observable.prototype.pipe = function () {
  72. var operations = [];
  73. for (var _i = 0; _i < arguments.length; _i++) {
  74. operations[_i] = arguments[_i];
  75. }
  76. return pipeFromArray(operations)(this);
  77. };
  78. Observable.prototype.toPromise = function (promiseCtor) {
  79. var _this = this;
  80. promiseCtor = getPromiseCtor(promiseCtor);
  81. return new promiseCtor(function (resolve, reject) {
  82. var value;
  83. _this.subscribe(function (x) { return (value = x); }, function (err) { return reject(err); }, function () { return resolve(value); });
  84. });
  85. };
  86. Observable.create = function (subscribe) {
  87. return new Observable(subscribe);
  88. };
  89. return Observable;
  90. }());
  91. export { Observable };
  92. function getPromiseCtor(promiseCtor) {
  93. var _a;
  94. return (_a = promiseCtor !== null && promiseCtor !== void 0 ? promiseCtor : config.Promise) !== null && _a !== void 0 ? _a : Promise;
  95. }
  96. function isObserver(value) {
  97. return value && isFunction(value.next) && isFunction(value.error) && isFunction(value.complete);
  98. }
  99. function isSubscriber(value) {
  100. return (value && value instanceof Subscriber) || (isObserver(value) && isSubscription(value));
  101. }
  102. //# sourceMappingURL=Observable.js.map