UNPKG

cbon

Version:
61 lines (60 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); class RecursiveCtx { get(_, result) { return result; } call(fn, ...args) { return () => fn(this, ...args); } } exports.RecursiveCtx = RecursiveCtx; const _AsyncRecursiveCtx = RecursiveCtx; exports.AsyncRecursiveCtx = _AsyncRecursiveCtx; function doRecursive(fn, ...args) { var _a, _b; const ctx = new RecursiveCtx; const gens = [fn(ctx, ...args)]; let v = undefined; while (gens.length > 0) { const gen = gens[gens.length - 1]; const now = gen.next(v); v = undefined; if (now.done) { gens.pop(); v = now.value; } else { const ngen = (_b = (_a = now).value) === null || _b === void 0 ? void 0 : _b.call(_a); if (ngen != null) { gens.push(ngen); } } } return v; } exports.doRecursive = doRecursive; async function doRecursiveAsync(fn, ...args) { var _a, _b; const ctx = new RecursiveCtx; const gens = [fn(ctx, ...args)]; let v = undefined; do { const gen = gens[gens.length - 1]; const now = await gen.next(v); v = undefined; if (now.done) { gens.pop(); v = now.value; } else { const ngen = (_b = (_a = now).value) === null || _b === void 0 ? void 0 : _b.call(_a); if (ngen != null) { gens.push(ngen); } } } while (gens.length > 0 ? (await utils_1.next_micro_tick(), true) : false); return v; } exports.doRecursiveAsync = doRecursiveAsync;