UNPKG

@graphql-mesh/utils

Version:
36 lines (35 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withCancel = void 0; function withCancel(asyncIterable, onCancel) { return new Proxy(asyncIterable, { get(asyncIterable, prop) { if (prop === Symbol.asyncIterator) { return function getIteratorWithCancel() { const asyncIterator = asyncIterable[Symbol.asyncIterator](); return { next: asyncIterator.next ? (...args) => asyncIterator.next(...args) : undefined, return: async (...args) => { onCancel(); if (asyncIterator.return) { return asyncIterator.return(...args); } return { value: undefined, done: true, }; }, throw: asyncIterator.throw ? (...args) => asyncIterator.throw(...args) : undefined, }; }; } const propVal = asyncIterable[prop]; if (typeof propVal === 'function') { // eslint-disable-next-line @typescript-eslint/ban-types return propVal.bind(asyncIterable); } return propVal; }, }); } exports.withCancel = withCancel;