iter.cjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.consumeAsyncIterableInContext = exports.consumeIteratorInContext = exports.isAsyncIterable = exports.isIterator = exports.isIterableIterator = void 0;
  4. const index_js_1 = require("../singletons/index.cjs");
  5. const config_js_1 = require("./config.cjs");
  6. function isIterableIterator(thing) {
  7. return (typeof thing === "object" &&
  8. thing !== null &&
  9. typeof thing[Symbol.iterator] === "function" &&
  10. // avoid detecting array/set as iterator
  11. typeof thing.next === "function");
  12. }
  13. exports.isIterableIterator = isIterableIterator;
  14. const isIterator = (x) => x != null &&
  15. typeof x === "object" &&
  16. "next" in x &&
  17. typeof x.next === "function";
  18. exports.isIterator = isIterator;
  19. function isAsyncIterable(thing) {
  20. return (typeof thing === "object" &&
  21. thing !== null &&
  22. typeof thing[Symbol.asyncIterator] ===
  23. "function");
  24. }
  25. exports.isAsyncIterable = isAsyncIterable;
  26. function* consumeIteratorInContext(context, iter) {
  27. while (true) {
  28. const { value, done } = index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(context), iter.next.bind(iter), true);
  29. if (done) {
  30. break;
  31. }
  32. else {
  33. yield value;
  34. }
  35. }
  36. }
  37. exports.consumeIteratorInContext = consumeIteratorInContext;
  38. async function* consumeAsyncIterableInContext(context, iter) {
  39. const iterator = iter[Symbol.asyncIterator]();
  40. while (true) {
  41. const { value, done } = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(context), iterator.next.bind(iter), true);
  42. if (done) {
  43. break;
  44. }
  45. else {
  46. yield value;
  47. }
  48. }
  49. }
  50. exports.consumeAsyncIterableInContext = consumeAsyncIterableInContext;