@graphql-mesh/utils
Version: 
32 lines (31 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withCancel = void 0;
function withCancel(asyncIterable, onCancel) {
    return new Proxy(asyncIterable, {
        get(asyncIterable, prop) {
            var _a;
            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,
                    };
                };
            }
            return (_a = asyncIterable[prop]) === null || _a === void 0 ? void 0 : _a.bind(asyncIterable);
        },
    });
}
exports.withCancel = withCancel;