UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

90 lines (89 loc) • 2.1 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { randomNumberBetween as f } from "../utils/math.js"; function v(P, n, u) { return new Promise((b, d) => { let i, r = f(u.minDuration, u.maxDuration); const a = Date.now(), m = !0, l = 1; let c = 0; const s = () => { clearTimeout(i), d(n.reason); }; if (n.aborted) { s(); return; } n.addEventListener("abort", s, { once: !0 }), P({ lengthComputable: m, loaded: c, total: l }); const e = () => { if (n.aborted) { s(); return; } const t = Date.now() - a; let o = f(u.minStep, u.maxStep); if (t + o > r && (o = t + o - r), c = t / r * l, P({ lengthComputable: m, loaded: Math.min(c, l), total: l }), c >= l) return n.removeEventListener("abort", s), b(); i = setTimeout(e, o); }; e(); }); } function h(P, n) { return async function(u, { onprogress: b, signal: d }) { const i = new AbortController(); let r, a; function m() { if (!a || !r) return; const e = r.loaded / r.total, t = a.loaded / a.total; if (e < t) return b({ ...r, lengthComputable: !0 }); b(a); } const l = v( (e) => { const t = n?.total || a?.total || 100; r = { lengthComputable: !0, loaded: Math.round(e.loaded * t), total: t }, m(); }, // if we abort we abort simulation as well i.signal, n ), c = () => { i.abort(d.reason); }; d.aborted ? c() : d.addEventListener("abort", c, { once: !0 }); const s = P(u, { onprogress: (e) => { a = e, m(); }, signal: d }); return new Promise((e, t) => { Promise.all([s, l]).then((o) => { e(o[0]); }).catch((o) => { i.abort(o), t(o); }).finally(() => { d.removeEventListener("abort", c); }); }); }; } export { h as createPerceivedPerformanceProxy };