innerFrom.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (_) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. var __asyncValues = (this && this.__asyncValues) || function (o) {
  39. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  40. var m = o[Symbol.asyncIterator], i;
  41. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  42. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  43. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  44. };
  45. var __values = (this && this.__values) || function(o) {
  46. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  47. if (m) return m.call(o);
  48. if (o && typeof o.length === "number") return {
  49. next: function () {
  50. if (o && i >= o.length) o = void 0;
  51. return { value: o && o[i++], done: !o };
  52. }
  53. };
  54. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  55. };
  56. Object.defineProperty(exports, "__esModule", { value: true });
  57. exports.fromReadableStreamLike = exports.fromAsyncIterable = exports.fromIterable = exports.fromPromise = exports.fromArrayLike = exports.fromInteropObservable = exports.innerFrom = void 0;
  58. var isArrayLike_1 = require("../util/isArrayLike");
  59. var isPromise_1 = require("../util/isPromise");
  60. var Observable_1 = require("../Observable");
  61. var isInteropObservable_1 = require("../util/isInteropObservable");
  62. var isAsyncIterable_1 = require("../util/isAsyncIterable");
  63. var throwUnobservableError_1 = require("../util/throwUnobservableError");
  64. var isIterable_1 = require("../util/isIterable");
  65. var isReadableStreamLike_1 = require("../util/isReadableStreamLike");
  66. var isFunction_1 = require("../util/isFunction");
  67. var reportUnhandledError_1 = require("../util/reportUnhandledError");
  68. var observable_1 = require("../symbol/observable");
  69. function innerFrom(input) {
  70. if (input instanceof Observable_1.Observable) {
  71. return input;
  72. }
  73. if (input != null) {
  74. if (isInteropObservable_1.isInteropObservable(input)) {
  75. return fromInteropObservable(input);
  76. }
  77. if (isArrayLike_1.isArrayLike(input)) {
  78. return fromArrayLike(input);
  79. }
  80. if (isPromise_1.isPromise(input)) {
  81. return fromPromise(input);
  82. }
  83. if (isAsyncIterable_1.isAsyncIterable(input)) {
  84. return fromAsyncIterable(input);
  85. }
  86. if (isIterable_1.isIterable(input)) {
  87. return fromIterable(input);
  88. }
  89. if (isReadableStreamLike_1.isReadableStreamLike(input)) {
  90. return fromReadableStreamLike(input);
  91. }
  92. }
  93. throw throwUnobservableError_1.createInvalidObservableTypeError(input);
  94. }
  95. exports.innerFrom = innerFrom;
  96. function fromInteropObservable(obj) {
  97. return new Observable_1.Observable(function (subscriber) {
  98. var obs = obj[observable_1.observable]();
  99. if (isFunction_1.isFunction(obs.subscribe)) {
  100. return obs.subscribe(subscriber);
  101. }
  102. throw new TypeError('Provided object does not correctly implement Symbol.observable');
  103. });
  104. }
  105. exports.fromInteropObservable = fromInteropObservable;
  106. function fromArrayLike(array) {
  107. return new Observable_1.Observable(function (subscriber) {
  108. for (var i = 0; i < array.length && !subscriber.closed; i++) {
  109. subscriber.next(array[i]);
  110. }
  111. subscriber.complete();
  112. });
  113. }
  114. exports.fromArrayLike = fromArrayLike;
  115. function fromPromise(promise) {
  116. return new Observable_1.Observable(function (subscriber) {
  117. promise
  118. .then(function (value) {
  119. if (!subscriber.closed) {
  120. subscriber.next(value);
  121. subscriber.complete();
  122. }
  123. }, function (err) { return subscriber.error(err); })
  124. .then(null, reportUnhandledError_1.reportUnhandledError);
  125. });
  126. }
  127. exports.fromPromise = fromPromise;
  128. function fromIterable(iterable) {
  129. return new Observable_1.Observable(function (subscriber) {
  130. var e_1, _a;
  131. try {
  132. for (var iterable_1 = __values(iterable), iterable_1_1 = iterable_1.next(); !iterable_1_1.done; iterable_1_1 = iterable_1.next()) {
  133. var value = iterable_1_1.value;
  134. subscriber.next(value);
  135. if (subscriber.closed) {
  136. return;
  137. }
  138. }
  139. }
  140. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  141. finally {
  142. try {
  143. if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) _a.call(iterable_1);
  144. }
  145. finally { if (e_1) throw e_1.error; }
  146. }
  147. subscriber.complete();
  148. });
  149. }
  150. exports.fromIterable = fromIterable;
  151. function fromAsyncIterable(asyncIterable) {
  152. return new Observable_1.Observable(function (subscriber) {
  153. process(asyncIterable, subscriber).catch(function (err) { return subscriber.error(err); });
  154. });
  155. }
  156. exports.fromAsyncIterable = fromAsyncIterable;
  157. function fromReadableStreamLike(readableStream) {
  158. return fromAsyncIterable(isReadableStreamLike_1.readableStreamLikeToAsyncGenerator(readableStream));
  159. }
  160. exports.fromReadableStreamLike = fromReadableStreamLike;
  161. function process(asyncIterable, subscriber) {
  162. var asyncIterable_1, asyncIterable_1_1;
  163. var e_2, _a;
  164. return __awaiter(this, void 0, void 0, function () {
  165. var value, e_2_1;
  166. return __generator(this, function (_b) {
  167. switch (_b.label) {
  168. case 0:
  169. _b.trys.push([0, 5, 6, 11]);
  170. asyncIterable_1 = __asyncValues(asyncIterable);
  171. _b.label = 1;
  172. case 1: return [4, asyncIterable_1.next()];
  173. case 2:
  174. if (!(asyncIterable_1_1 = _b.sent(), !asyncIterable_1_1.done)) return [3, 4];
  175. value = asyncIterable_1_1.value;
  176. subscriber.next(value);
  177. if (subscriber.closed) {
  178. return [2];
  179. }
  180. _b.label = 3;
  181. case 3: return [3, 1];
  182. case 4: return [3, 11];
  183. case 5:
  184. e_2_1 = _b.sent();
  185. e_2 = { error: e_2_1 };
  186. return [3, 11];
  187. case 6:
  188. _b.trys.push([6, , 9, 10]);
  189. if (!(asyncIterable_1_1 && !asyncIterable_1_1.done && (_a = asyncIterable_1.return))) return [3, 8];
  190. return [4, _a.call(asyncIterable_1)];
  191. case 7:
  192. _b.sent();
  193. _b.label = 8;
  194. case 8: return [3, 10];
  195. case 9:
  196. if (e_2) throw e_2.error;
  197. return [7];
  198. case 10: return [7];
  199. case 11:
  200. subscriber.complete();
  201. return [2];
  202. }
  203. });
  204. });
  205. }
  206. //# sourceMappingURL=innerFrom.js.map