'use strict'; var solidJs = require('solid-js'); var utils = require('@solid-primitives/utils'); // src/index.ts exports.promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise( (resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms) ); function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") { return new Promise((resolve, reject) => { let resolved = false; const promises = utils.asArray(input); const timeout = exports.promiseTimeout(ms, throwOnTimeout, reason).catch((e) => { !resolved && reject(e); }); const race = Promise.race([...promises, timeout]); race.then(resolve, reject); race.finally(() => { resolved = true; promises.forEach( // inputted promises can have .dispose() method on them, // it will be called when the first promise resolves, to stop the rest (p) => { p && typeof p === "object" && typeof p.dispose === "function" && p.dispose(); } ); }); }); } exports.until = (condition) => { const promise = solidJs.createRoot((dispose) => { const memo = solidJs.createMemo(condition); const promise2 = new Promise((resolve, reject) => { solidJs.createComputed(() => { if (!memo()) return; resolve(memo()); dispose(); }); solidJs.onCleanup(reject); }); promise2.dispose = dispose; return promise2; }); solidJs.getOwner() && solidJs.onCleanup(promise.dispose); return promise; }; function changed(source, times = 1) { times += 1; return () => { source(); return !--times; }; } exports.changed = changed; exports.raceTimeout = raceTimeout;