Observable.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var canReportError_1 = require("./util/canReportError");
  4. var toSubscriber_1 = require("./util/toSubscriber");
  5. var observable_1 = require("./symbol/observable");
  6. var pipe_1 = require("./util/pipe");
  7. var config_1 = require("./config");
  8. var Observable = (function () {
  9. function Observable(subscribe) {
  10. this._isScalar = false;
  11. if (subscribe) {
  12. this._subscribe = subscribe;
  13. }
  14. }
  15. Observable.prototype.lift = function (operator) {
  16. var observable = new Observable();
  17. observable.source = this;
  18. observable.operator = operator;
  19. return observable;
  20. };
  21. Observable.prototype.subscribe = function (observerOrNext, error, complete) {
  22. var operator = this.operator;
  23. var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete);
  24. if (operator) {
  25. sink.add(operator.call(sink, this.source));
  26. }
  27. else {
  28. sink.add(this.source || (config_1.config.useDeprecatedSynchronousErrorHandling && !sink.syncErrorThrowable) ?
  29. this._subscribe(sink) :
  30. this._trySubscribe(sink));
  31. }
  32. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  33. if (sink.syncErrorThrowable) {
  34. sink.syncErrorThrowable = false;
  35. if (sink.syncErrorThrown) {
  36. throw sink.syncErrorValue;
  37. }
  38. }
  39. }
  40. return sink;
  41. };
  42. Observable.prototype._trySubscribe = function (sink) {
  43. try {
  44. return this._subscribe(sink);
  45. }
  46. catch (err) {
  47. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  48. sink.syncErrorThrown = true;
  49. sink.syncErrorValue = err;
  50. }
  51. if (canReportError_1.canReportError(sink)) {
  52. sink.error(err);
  53. }
  54. else {
  55. console.warn(err);
  56. }
  57. }
  58. };
  59. Observable.prototype.forEach = function (next, promiseCtor) {
  60. var _this = this;
  61. promiseCtor = getPromiseCtor(promiseCtor);
  62. return new promiseCtor(function (resolve, reject) {
  63. var subscription;
  64. subscription = _this.subscribe(function (value) {
  65. try {
  66. next(value);
  67. }
  68. catch (err) {
  69. reject(err);
  70. if (subscription) {
  71. subscription.unsubscribe();
  72. }
  73. }
  74. }, reject, resolve);
  75. });
  76. };
  77. Observable.prototype._subscribe = function (subscriber) {
  78. var source = this.source;
  79. return source && source.subscribe(subscriber);
  80. };
  81. Observable.prototype[observable_1.observable] = function () {
  82. return this;
  83. };
  84. Observable.prototype.pipe = function () {
  85. var operations = [];
  86. for (var _i = 0; _i < arguments.length; _i++) {
  87. operations[_i] = arguments[_i];
  88. }
  89. if (operations.length === 0) {
  90. return this;
  91. }
  92. return pipe_1.pipeFromArray(operations)(this);
  93. };
  94. Observable.prototype.toPromise = function (promiseCtor) {
  95. var _this = this;
  96. promiseCtor = getPromiseCtor(promiseCtor);
  97. return new promiseCtor(function (resolve, reject) {
  98. var value;
  99. _this.subscribe(function (x) { return value = x; }, function (err) { return reject(err); }, function () { return resolve(value); });
  100. });
  101. };
  102. Observable.create = function (subscribe) {
  103. return new Observable(subscribe);
  104. };
  105. return Observable;
  106. }());
  107. exports.Observable = Observable;
  108. function getPromiseCtor(promiseCtor) {
  109. if (!promiseCtor) {
  110. promiseCtor = config_1.config.Promise || Promise;
  111. }
  112. if (!promiseCtor) {
  113. throw new Error('no Promise impl found');
  114. }
  115. return promiseCtor;
  116. }
  117. //# sourceMappingURL=Observable.js.map