a zip code crypto-currency system good for red ONLY

PromiseObservable.js 3.9KB

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