asyncIterator.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const a = Object.getPrototypeOf(
  2. Object.getPrototypeOf(
  3. /* istanbul ignore next */
  4. async function* () {
  5. }
  6. ).prototype
  7. );
  8. class c {
  9. #t;
  10. #n;
  11. #r = !1;
  12. #e = void 0;
  13. constructor(e, t) {
  14. this.#t = e, this.#n = t;
  15. }
  16. next() {
  17. const e = () => this.#s();
  18. return this.#e = this.#e ? this.#e.then(e, e) : e(), this.#e;
  19. }
  20. return(e) {
  21. const t = () => this.#i(e);
  22. return this.#e ? this.#e.then(t, t) : t();
  23. }
  24. async #s() {
  25. if (this.#r)
  26. return {
  27. done: !0,
  28. value: void 0
  29. };
  30. let e;
  31. try {
  32. e = await this.#t.read();
  33. } catch (t) {
  34. throw this.#e = void 0, this.#r = !0, this.#t.releaseLock(), t;
  35. }
  36. return e.done && (this.#e = void 0, this.#r = !0, this.#t.releaseLock()), e;
  37. }
  38. async #i(e) {
  39. if (this.#r)
  40. return {
  41. done: !0,
  42. value: e
  43. };
  44. if (this.#r = !0, !this.#n) {
  45. const t = this.#t.cancel(e);
  46. return this.#t.releaseLock(), await t, {
  47. done: !0,
  48. value: e
  49. };
  50. }
  51. return this.#t.releaseLock(), {
  52. done: !0,
  53. value: e
  54. };
  55. }
  56. }
  57. const n = Symbol();
  58. function i() {
  59. return this[n].next();
  60. }
  61. Object.defineProperty(i, "name", { value: "next" });
  62. function o(r) {
  63. return this[n].return(r);
  64. }
  65. Object.defineProperty(o, "name", { value: "return" });
  66. const u = Object.create(a, {
  67. next: {
  68. enumerable: !0,
  69. configurable: !0,
  70. writable: !0,
  71. value: i
  72. },
  73. return: {
  74. enumerable: !0,
  75. configurable: !0,
  76. writable: !0,
  77. value: o
  78. }
  79. });
  80. function h({ preventCancel: r = !1 } = {}) {
  81. const e = this.getReader(), t = new c(
  82. e,
  83. r
  84. ), s = Object.create(u);
  85. return s[n] = t, s;
  86. }
  87. export {
  88. h as asyncIterator
  89. };