innerFrom.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { __asyncValues, __awaiter } from "tslib";
  2. import { isArrayLike } from '../util/isArrayLike';
  3. import { isPromise } from '../util/isPromise';
  4. import { Observable } from '../Observable';
  5. import { isInteropObservable } from '../util/isInteropObservable';
  6. import { isAsyncIterable } from '../util/isAsyncIterable';
  7. import { createInvalidObservableTypeError } from '../util/throwUnobservableError';
  8. import { isIterable } from '../util/isIterable';
  9. import { isReadableStreamLike, readableStreamLikeToAsyncGenerator } from '../util/isReadableStreamLike';
  10. import { isFunction } from '../util/isFunction';
  11. import { reportUnhandledError } from '../util/reportUnhandledError';
  12. import { observable as Symbol_observable } from '../symbol/observable';
  13. export function innerFrom(input) {
  14. if (input instanceof Observable) {
  15. return input;
  16. }
  17. if (input != null) {
  18. if (isInteropObservable(input)) {
  19. return fromInteropObservable(input);
  20. }
  21. if (isArrayLike(input)) {
  22. return fromArrayLike(input);
  23. }
  24. if (isPromise(input)) {
  25. return fromPromise(input);
  26. }
  27. if (isAsyncIterable(input)) {
  28. return fromAsyncIterable(input);
  29. }
  30. if (isIterable(input)) {
  31. return fromIterable(input);
  32. }
  33. if (isReadableStreamLike(input)) {
  34. return fromReadableStreamLike(input);
  35. }
  36. }
  37. throw createInvalidObservableTypeError(input);
  38. }
  39. export function fromInteropObservable(obj) {
  40. return new Observable((subscriber) => {
  41. const obs = obj[Symbol_observable]();
  42. if (isFunction(obs.subscribe)) {
  43. return obs.subscribe(subscriber);
  44. }
  45. throw new TypeError('Provided object does not correctly implement Symbol.observable');
  46. });
  47. }
  48. export function fromArrayLike(array) {
  49. return new Observable((subscriber) => {
  50. for (let i = 0; i < array.length && !subscriber.closed; i++) {
  51. subscriber.next(array[i]);
  52. }
  53. subscriber.complete();
  54. });
  55. }
  56. export function fromPromise(promise) {
  57. return new Observable((subscriber) => {
  58. promise
  59. .then((value) => {
  60. if (!subscriber.closed) {
  61. subscriber.next(value);
  62. subscriber.complete();
  63. }
  64. }, (err) => subscriber.error(err))
  65. .then(null, reportUnhandledError);
  66. });
  67. }
  68. export function fromIterable(iterable) {
  69. return new Observable((subscriber) => {
  70. for (const value of iterable) {
  71. subscriber.next(value);
  72. if (subscriber.closed) {
  73. return;
  74. }
  75. }
  76. subscriber.complete();
  77. });
  78. }
  79. export function fromAsyncIterable(asyncIterable) {
  80. return new Observable((subscriber) => {
  81. process(asyncIterable, subscriber).catch((err) => subscriber.error(err));
  82. });
  83. }
  84. export function fromReadableStreamLike(readableStream) {
  85. return fromAsyncIterable(readableStreamLikeToAsyncGenerator(readableStream));
  86. }
  87. function process(asyncIterable, subscriber) {
  88. var asyncIterable_1, asyncIterable_1_1;
  89. var e_1, _a;
  90. return __awaiter(this, void 0, void 0, function* () {
  91. try {
  92. for (asyncIterable_1 = __asyncValues(asyncIterable); asyncIterable_1_1 = yield asyncIterable_1.next(), !asyncIterable_1_1.done;) {
  93. const value = asyncIterable_1_1.value;
  94. subscriber.next(value);
  95. if (subscriber.closed) {
  96. return;
  97. }
  98. }
  99. }
  100. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  101. finally {
  102. try {
  103. if (asyncIterable_1_1 && !asyncIterable_1_1.done && (_a = asyncIterable_1.return)) yield _a.call(asyncIterable_1);
  104. }
  105. finally { if (e_1) throw e_1.error; }
  106. }
  107. subscriber.complete();
  108. });
  109. }
  110. //# sourceMappingURL=innerFrom.js.map