PromiseObservable.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /** PURE_IMPORTS_START .._util_root,.._Observable PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { root } from '../util/root';
  10. import { Observable } from '../Observable';
  11. /**
  12. * We need this JSDoc comment for affecting ESDoc.
  13. * @extends {Ignored}
  14. * @hide true
  15. */
  16. export var PromiseObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  17. __extends(PromiseObservable, _super);
  18. function PromiseObservable(promise, scheduler) {
  19. _super.call(this);
  20. this.promise = promise;
  21. this.scheduler = scheduler;
  22. }
  23. /**
  24. * Converts a Promise to an Observable.
  25. *
  26. * <span class="informal">Returns an Observable that just emits the Promise's
  27. * resolved value, then completes.</span>
  28. *
  29. * Converts an ES2015 Promise or a Promises/A+ spec compliant Promise to an
  30. * Observable. If the Promise resolves with a value, the output Observable
  31. * emits that resolved value as a `next`, and then completes. If the Promise
  32. * is rejected, then the output Observable emits the corresponding Error.
  33. *
  34. * @example <caption>Convert the Promise returned by Fetch to an Observable</caption>
  35. * var result = Rx.Observable.fromPromise(fetch('http://myserver.com/'));
  36. * result.subscribe(x => console.log(x), e => console.error(e));
  37. *
  38. * @see {@link bindCallback}
  39. * @see {@link from}
  40. *
  41. * @param {PromiseLike<T>} promise The promise to be converted.
  42. * @param {Scheduler} [scheduler] An optional IScheduler to use for scheduling
  43. * the delivery of the resolved value (or the rejection).
  44. * @return {Observable<T>} An Observable which wraps the Promise.
  45. * @static true
  46. * @name fromPromise
  47. * @owner Observable
  48. */
  49. PromiseObservable.create = function (promise, scheduler) {
  50. return new PromiseObservable(promise, scheduler);
  51. };
  52. /** @deprecated internal use only */ PromiseObservable.prototype._subscribe = function (subscriber) {
  53. var _this = this;
  54. var promise = this.promise;
  55. var scheduler = this.scheduler;
  56. if (scheduler == null) {
  57. if (this._isScalar) {
  58. if (!subscriber.closed) {
  59. subscriber.next(this.value);
  60. subscriber.complete();
  61. }
  62. }
  63. else {
  64. promise.then(function (value) {
  65. _this.value = value;
  66. _this._isScalar = true;
  67. if (!subscriber.closed) {
  68. subscriber.next(value);
  69. subscriber.complete();
  70. }
  71. }, function (err) {
  72. if (!subscriber.closed) {
  73. subscriber.error(err);
  74. }
  75. })
  76. .then(null, function (err) {
  77. // escape the promise trap, throw unhandled errors
  78. root.setTimeout(function () { throw err; });
  79. });
  80. }
  81. }
  82. else {
  83. if (this._isScalar) {
  84. if (!subscriber.closed) {
  85. return scheduler.schedule(dispatchNext, 0, { value: this.value, subscriber: subscriber });
  86. }
  87. }
  88. else {
  89. promise.then(function (value) {
  90. _this.value = value;
  91. _this._isScalar = true;
  92. if (!subscriber.closed) {
  93. subscriber.add(scheduler.schedule(dispatchNext, 0, { value: value, subscriber: subscriber }));
  94. }
  95. }, function (err) {
  96. if (!subscriber.closed) {
  97. subscriber.add(scheduler.schedule(dispatchError, 0, { err: err, subscriber: subscriber }));
  98. }
  99. })
  100. .then(null, function (err) {
  101. // escape the promise trap, throw unhandled errors
  102. root.setTimeout(function () { throw err; });
  103. });
  104. }
  105. }
  106. };
  107. return PromiseObservable;
  108. }(Observable));
  109. function dispatchNext(arg) {
  110. var value = arg.value, subscriber = arg.subscriber;
  111. if (!subscriber.closed) {
  112. subscriber.next(value);
  113. subscriber.complete();
  114. }
  115. }
  116. function dispatchError(arg) {
  117. var err = arg.err, subscriber = arg.subscriber;
  118. if (!subscriber.closed) {
  119. subscriber.error(err);
  120. }
  121. }
  122. //# sourceMappingURL=PromiseObservable.js.map