Observable.js 3.9 KB

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