PromiseObservable.js 4.6 KB

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