@php-wasm/progress
Advanced tools
Comparing version 0.3.1 to 0.4.0
271
index.js
@@ -1,86 +0,108 @@ | ||
class f extends EventTarget { | ||
#e = {}; | ||
#t = {}; | ||
constructor(e = []) { | ||
super(), this.setModules(e), this.#s(); | ||
var L = (i, e, t) => { | ||
if (!e.has(i)) | ||
throw TypeError("Cannot " + t); | ||
}; | ||
var l = (i, e, t) => (L(i, e, "read from private field"), t ? t.call(i) : e.get(i)), d = (i, e, t) => { | ||
if (e.has(i)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
e instanceof WeakSet ? e.add(i) : e.set(i, t); | ||
}, _ = (i, e, t, s) => (L(i, e, "write to private field"), s ? s.call(i, t) : e.set(i, t), t); | ||
var P = (i, e, t, s) => ({ | ||
set _(n) { | ||
_(i, e, n, t); | ||
}, | ||
get _() { | ||
return l(i, e, s); | ||
} | ||
}), g = (i, e, t) => (L(i, e, "access private method"), t); | ||
var f, p, y, O, w, R, m, b; | ||
class A extends EventTarget { | ||
constructor(t = []) { | ||
super(); | ||
/** | ||
* Replaces the default WebAssembly.instantiateStreaming with a version | ||
* that monitors the download #progress. | ||
*/ | ||
d(this, y); | ||
/** | ||
* Creates a `dataFileDownloads` Proxy object that can be passed | ||
* to `startPHP` to monitor the download #progress of the data | ||
* dependencies. | ||
*/ | ||
d(this, w); | ||
/** | ||
* Notifies about the download #progress of a file. | ||
* | ||
* @param file The file name. | ||
* @param loaded The number of bytes of that file loaded so far. | ||
* @param fileSize The total number of bytes in the loaded file. | ||
*/ | ||
d(this, m); | ||
d(this, f, {}); | ||
d(this, p, {}); | ||
this.setModules(t), g(this, y, O).call(this); | ||
} | ||
getEmscriptenOptions() { | ||
return { | ||
dataFileDownloads: this.#r() | ||
dataFileDownloads: g(this, w, R).call(this) | ||
}; | ||
} | ||
setModules(e) { | ||
this.#e = e.reduce((t, s) => { | ||
if (s.dependenciesTotalSize > 0) { | ||
const i = "http://example.com/", n = new URL(s.dependencyFilename, i).pathname.split("/").pop(); | ||
t[n] = Math.max( | ||
n in t ? t[n] : 0, | ||
s.dependenciesTotalSize | ||
setModules(t) { | ||
_(this, f, t.reduce((s, n) => { | ||
if (n.dependenciesTotalSize > 0) { | ||
const r = "http://example.com/", o = new URL(n.dependencyFilename, r).pathname.split("/").pop(); | ||
s[o] = Math.max( | ||
o in s ? s[o] : 0, | ||
n.dependenciesTotalSize | ||
); | ||
} | ||
return t; | ||
}, {}), this.#t = Object.fromEntries( | ||
Object.entries(this.#e).map(([t]) => [t, 0]) | ||
return s; | ||
}, {})), _(this, p, Object.fromEntries( | ||
Object.entries(l(this, f)).map(([s]) => [s, 0]) | ||
)); | ||
} | ||
} | ||
f = new WeakMap(), p = new WeakMap(), y = new WeakSet(), O = function() { | ||
const t = WebAssembly.instantiateStreaming; | ||
WebAssembly.instantiateStreaming = async (s, ...n) => { | ||
const r = await s, a = r.url.substring( | ||
new URL(r.url).origin.length + 1 | ||
), o = D( | ||
r, | ||
({ detail: { loaded: h, total: c } }) => g(this, m, b).call(this, a, h, c) | ||
); | ||
} | ||
/** | ||
* Replaces the default WebAssembly.instantiateStreaming with a version | ||
* that monitors the download #progress. | ||
*/ | ||
#s() { | ||
const e = WebAssembly.instantiateStreaming; | ||
WebAssembly.instantiateStreaming = async (t, ...s) => { | ||
const i = await t, r = i.url.substring( | ||
new URL(i.url).origin.length + 1 | ||
), n = c( | ||
i, | ||
({ detail: { loaded: a, total: l } }) => this.#i(r, a, l) | ||
); | ||
return e(n, ...s); | ||
}; | ||
} | ||
/** | ||
* Creates a `dataFileDownloads` Proxy object that can be passed | ||
* to `startPHP` to monitor the download #progress of the data | ||
* dependencies. | ||
*/ | ||
#r() { | ||
const e = this, t = {}; | ||
return new Proxy(t, { | ||
set(s, i, r) { | ||
return e.#i(i, r.loaded, r.total), s[i] = new Proxy(JSON.parse(JSON.stringify(r)), { | ||
set(n, a, l) { | ||
return n[a] = l, e.#i(i, n.loaded, n.total), !0; | ||
} | ||
}), !0; | ||
return t(o, ...n); | ||
}; | ||
}, w = new WeakSet(), R = function() { | ||
const t = this, s = {}; | ||
return new Proxy(s, { | ||
set(n, r, a) { | ||
var o; | ||
return g(o = t, m, b).call(o, r, a.loaded, a.total), n[r] = new Proxy(JSON.parse(JSON.stringify(a)), { | ||
set(h, c, F) { | ||
var S; | ||
return h[c] = F, g(S = t, m, b).call(S, r, h.loaded, h.total), !0; | ||
} | ||
}), !0; | ||
} | ||
}); | ||
}, m = new WeakSet(), b = function(t, s, n) { | ||
const r = new URL(t, "http://example.com").pathname.split("/").pop(); | ||
n || (n = l(this, f)[r]), r in l(this, p) || console.warn( | ||
`Registered a download #progress of an unregistered file "${r}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.` | ||
), l(this, p)[r] = s, this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
loaded: C(l(this, p)), | ||
total: C(l(this, f)) | ||
} | ||
}); | ||
} | ||
/** | ||
* Notifies about the download #progress of a file. | ||
* | ||
* @param file The file name. | ||
* @param loaded The number of bytes of that file loaded so far. | ||
* @param fileSize The total number of bytes in the loaded file. | ||
*/ | ||
#i(e, t, s) { | ||
const i = new URL(e, "http://example.com").pathname.split("/").pop(); | ||
s || (s = this.#e[i]), i in this.#t || console.warn( | ||
`Registered a download #progress of an unregistered file "${i}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.` | ||
), this.#t[i] = t, this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
loaded: g(this.#t), | ||
total: g(this.#e) | ||
} | ||
}) | ||
); | ||
} | ||
}) | ||
); | ||
}; | ||
function C(i) { | ||
return Object.values(i).reduce((e, t) => e + t, 0); | ||
} | ||
function g(o) { | ||
return Object.values(o).reduce((e, t) => e + t, 0); | ||
} | ||
function c(o, e) { | ||
const t = o.headers.get("content-length") || "", s = parseInt(t, 10) || 5242880; | ||
function i(r, n) { | ||
function D(i, e) { | ||
const t = i.headers.get("content-length") || "", s = parseInt(t, 10) || 5242880; | ||
function n(r, a) { | ||
e( | ||
@@ -90,3 +112,3 @@ new CustomEvent("progress", { | ||
loaded: r, | ||
total: n | ||
total: a | ||
} | ||
@@ -99,18 +121,18 @@ }) | ||
async start(r) { | ||
if (!o.body) { | ||
if (!i.body) { | ||
r.close(); | ||
return; | ||
} | ||
const n = o.body.getReader(); | ||
let a = 0; | ||
const a = i.body.getReader(); | ||
let o = 0; | ||
for (; ; ) | ||
try { | ||
const { done: l, value: h } = await n.read(); | ||
if (h && (a += h.byteLength), l) { | ||
i(a, a), r.close(); | ||
const { done: h, value: c } = await a.read(); | ||
if (c && (o += c.byteLength), h) { | ||
n(o, o), r.close(); | ||
break; | ||
} else | ||
i(a, s), r.enqueue(h); | ||
} catch (l) { | ||
console.error({ e: l }), r.error(l); | ||
n(o, s), r.enqueue(c); | ||
} catch (h) { | ||
console.error({ e: h }), r.error(h); | ||
break; | ||
@@ -121,45 +143,48 @@ } | ||
{ | ||
status: o.status, | ||
statusText: o.statusText, | ||
headers: o.headers | ||
status: i.status, | ||
statusText: i.statusText, | ||
headers: i.headers | ||
} | ||
); | ||
} | ||
class p extends EventTarget { | ||
var u, v, E, I; | ||
class W extends EventTarget { | ||
constructor() { | ||
super(...arguments), this.#e = {}, this.#t = 0, this.progress = 0, this.mode = "REAL_TIME", this.caption = ""; | ||
super(...arguments); | ||
d(this, E); | ||
d(this, u, void 0); | ||
d(this, v, void 0); | ||
_(this, u, {}), _(this, v, 0), this.progress = 0, this.mode = "REAL_TIME", this.caption = ""; | ||
} | ||
#e; | ||
#t; | ||
partialObserver(e, t = "") { | ||
const s = ++this.#t; | ||
return this.#e[s] = 0, (i) => { | ||
const { loaded: r, total: n } = i?.detail || {}; | ||
this.#e[s] = r / n * e, this.#s(this.totalProgress, "REAL_TIME", t); | ||
partialObserver(t, s = "") { | ||
const n = ++P(this, v)._; | ||
return l(this, u)[n] = 0, (r) => { | ||
const { loaded: a, total: o } = (r == null ? void 0 : r.detail) || {}; | ||
l(this, u)[n] = a / o * t, g(this, E, I).call(this, this.totalProgress, "REAL_TIME", s); | ||
}; | ||
} | ||
slowlyIncrementBy(e) { | ||
const t = ++this.#t; | ||
this.#e[t] = e, this.#s(this.totalProgress, "SLOWLY_INCREMENT"); | ||
slowlyIncrementBy(t) { | ||
const s = ++P(this, v)._; | ||
l(this, u)[s] = t, g(this, E, I).call(this, this.totalProgress, "SLOWLY_INCREMENT"); | ||
} | ||
get totalProgress() { | ||
return Object.values(this.#e).reduce( | ||
(e, t) => e + t, | ||
return Object.values(l(this, u)).reduce( | ||
(t, s) => t + s, | ||
0 | ||
); | ||
} | ||
#s(e, t, s) { | ||
this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
progress: e, | ||
mode: t, | ||
caption: s | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
const d = 1e-5; | ||
class u extends EventTarget { | ||
u = new WeakMap(), v = new WeakMap(), E = new WeakSet(), I = function(t, s, n) { | ||
this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
progress: t, | ||
mode: s, | ||
caption: n | ||
} | ||
}) | ||
); | ||
}; | ||
const T = 1e-5; | ||
class x extends EventTarget { | ||
constructor({ | ||
@@ -208,3 +233,3 @@ weight: e = 1, | ||
stage(e, t = "") { | ||
if (e || (e = this._selfWeight), this._selfWeight - e < -d) | ||
if (e || (e = this._selfWeight), this._selfWeight - e < -T) | ||
throw new Error( | ||
@@ -214,3 +239,3 @@ `Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.` | ||
this._selfWeight -= e; | ||
const s = new u({ | ||
const s = new x({ | ||
caption: t, | ||
@@ -252,3 +277,3 @@ weight: e, | ||
set(e) { | ||
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + d >= 100 && this.finish(); | ||
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + T >= 100 && this.finish(); | ||
} | ||
@@ -271,3 +296,3 @@ finish() { | ||
get done() { | ||
return this.progress + d >= 100; | ||
return this.progress + T >= 100; | ||
} | ||
@@ -335,6 +360,6 @@ get progress() { | ||
export { | ||
f as EmscriptenDownloadMonitor, | ||
p as ProgressObserver, | ||
u as ProgressTracker, | ||
c as cloneResponseMonitorProgress | ||
A as EmscriptenDownloadMonitor, | ||
W as ProgressObserver, | ||
x as ProgressTracker, | ||
D as cloneResponseMonitorProgress | ||
}; |
{ | ||
"name": "@php-wasm/progress", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "PHP.wasm – loading progress monitoring", | ||
@@ -32,3 +32,3 @@ "repository": { | ||
"types": "index.d.ts", | ||
"gitHead": "73b285f8b496c7442474fd7325b3f6b1287cbf95", | ||
"gitHead": "0851e71512709843af0539f747caded8ea904fe6", | ||
"engines": { | ||
@@ -35,0 +35,0 @@ "node": ">=16.15.1", |
Sorry, the diff of this file is not supported yet
26679
632