UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

101 lines (100 loc) • 3.12 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { createStoreExtension as L } from "./common/createStoreExtension.js"; import { isFileEntry as k, isFile as P } from "../utils/test.js"; import { getUniqueId as U } from "../utils/string.js"; import { createProgressEvent as z } from "../utils/xhr.js"; import { log as b } from "../common/console.js"; import { sleep as h } from "../utils/sleep.js"; import { noop as v } from "../utils/placeholder.js"; const C = L({ name: "SimulatedStore", props: { bitrate: 1024e3, tickrate: 250, connectionDelay: 250, fetchStoredFile: void 0, log: !0 }, factory: ({ extensionName: E, props: y, didSetProps: S }) => { let p; S(({ bitrate: e = 1024e3, tickrate: o = 250 }) => { p = e / 8 * (o / 1e3); }); const l = /* @__PURE__ */ new Map(), g = async (e, { onprogress: o, signal: r }) => { if (!k(e) || !P(e.file)) return; const { log: t, connectionDelay: a, tickrate: s, onstore: c = v } = y; if (await h(a), r.aborted) throw r.reason; const n = e.size; return await new Promise((D, w) => { let u, d = 0; const m = () => { r.removeEventListener("abort", m), clearInterval(u), w(r.reason); }; r.addEventListener("abort", m, { once: !0 }), u = setInterval(() => { if (r.aborted) { m(); return; } d = Math.min(n, d + p); try { c(d / n); } catch (f) { t && i(["error during store operation"]), clearInterval(u), r.removeEventListener("abort", m), w(f); return; } if (o(z(!0, d, n)), d === n) { const f = U(); l.set(f, e), t && i(["did store", f]), clearInterval(u), r.removeEventListener("abort", m), D(f); } }, s); }); }, F = async (e, o, r) => { const { log: t, connectionDelay: a, fetchStoredFile: s = () => new File(["Simulated"], "Untitled.txt", { type: "plain/text" }), onrestore: c = v } = y; if (await h(a), r.signal.aborted) throw r.signal.reason; try { c(); } catch (n) { throw t && i(["error during restore operation"]), n; } return l.has(e) ? (t && i(["did restore", e]), l.get(e)) : await s(e, o, r); }, x = async (e, o, r) => { const { log: t, connectionDelay: a, onrelease: s = v } = y; await h(a); const { signal: c } = r ?? {}; if (c?.aborted) throw c.reason; try { s(); } catch (n) { throw t && i(["error during release operation"]), n; } return l.delete(e), t && i(["did release", e]), !0; }; function i(e) { b("⛃", E, "(", ...e, ")"), Array.from(l).forEach(([o, r], t, a) => { b(" ", t < a.length - 1 ? "├─" : "└─", o, r); }); } return { storeEntry: g, restoreEntry: F, releaseEntry: x }; } }); export { C as SimulatedStore };