@php-wasm/progress
Advanced tools
Comparing version 0.1.18 to 0.1.26
export * from './lib/emscripten-download-monitor'; | ||
export * from './lib/progress-observer'; | ||
export * from './lib/progress-tracker'; |
299
index.js
@@ -1,24 +0,24 @@ | ||
class u extends EventTarget { | ||
class c extends EventTarget { | ||
#e = {}; | ||
#t = {}; | ||
constructor(t = []) { | ||
super(), this.setModules(t), this.#s(); | ||
constructor(e = []) { | ||
super(), this.setModules(e), this.#s(); | ||
} | ||
getEmscriptenOptions() { | ||
return { | ||
dataFileDownloads: this.#r() | ||
dataFileDownloads: this.#i() | ||
}; | ||
} | ||
setModules(t) { | ||
this.#e = t.reduce((e, n) => { | ||
if (n.dependenciesTotalSize > 0) { | ||
const s = "http://example.com/", a = new URL(n.dependencyFilename, s).pathname.split("/").pop(); | ||
e[a] = Math.max( | ||
a in e ? e[a] : 0, | ||
n.dependenciesTotalSize | ||
setModules(e) { | ||
this.#e = e.reduce((t, s) => { | ||
if (s.dependenciesTotalSize > 0) { | ||
const r = "http://example.com/", n = new URL(s.dependencyFilename, r).pathname.split("/").pop(); | ||
t[n] = Math.max( | ||
n in t ? t[n] : 0, | ||
s.dependenciesTotalSize | ||
); | ||
} | ||
return e; | ||
return t; | ||
}, {}), this.#t = Object.fromEntries( | ||
Object.entries(this.#e).map(([e]) => [e, 0]) | ||
Object.entries(this.#e).map(([t]) => [t, 0]) | ||
); | ||
@@ -31,11 +31,11 @@ } | ||
#s() { | ||
const t = WebAssembly.instantiateStreaming; | ||
WebAssembly.instantiateStreaming = async (e, ...n) => { | ||
const s = await e, r = s.url.substring( | ||
new URL(s.url).origin.length + 1 | ||
), a = h( | ||
s, | ||
({ detail: { loaded: i, total: l } }) => this.#n(r, i, l) | ||
const e = WebAssembly.instantiateStreaming; | ||
WebAssembly.instantiateStreaming = async (t, ...s) => { | ||
const r = await t, i = r.url.substring( | ||
new URL(r.url).origin.length + 1 | ||
), n = u( | ||
r, | ||
({ detail: { loaded: a, total: l } }) => this.#r(i, a, l) | ||
); | ||
return t(a, ...n); | ||
return e(n, ...s); | ||
}; | ||
@@ -48,9 +48,9 @@ } | ||
*/ | ||
#r() { | ||
const t = this, e = {}; | ||
return new Proxy(e, { | ||
set(n, s, r) { | ||
return t.#n(s, r.loaded, r.total), n[s] = new Proxy(JSON.parse(JSON.stringify(r)), { | ||
set(a, i, l) { | ||
return a[i] = l, t.#n(s, a.loaded, a.total), !0; | ||
#i() { | ||
const e = this, t = {}; | ||
return new Proxy(t, { | ||
set(s, r, i) { | ||
return e.#r(r, i.loaded, i.total), s[r] = new Proxy(JSON.parse(JSON.stringify(i)), { | ||
set(n, a, l) { | ||
return n[a] = l, e.#r(r, n.loaded, n.total), !0; | ||
} | ||
@@ -68,11 +68,11 @@ }), !0; | ||
*/ | ||
#n(t, e, n) { | ||
const s = new URL(t, "http://example.com").pathname.split("/").pop(); | ||
n || (n = this.#e[s]), s in this.#t || console.warn( | ||
`Registered a download #progress of an unregistered file "${s}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.` | ||
), this.#t[t] = e, this.dispatchEvent( | ||
#r(e, t, s) { | ||
const r = new URL(e, "http://example.com").pathname.split("/").pop(); | ||
s || (s = this.#e[r]), r in this.#t || 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.` | ||
), this.#t[e] = t, this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
loaded: c(this.#t), | ||
total: c(this.#e) | ||
loaded: d(this.#t), | ||
total: d(this.#e) | ||
} | ||
@@ -83,13 +83,13 @@ }) | ||
} | ||
function c(o) { | ||
return Object.values(o).reduce((t, e) => t + e, 0); | ||
function d(o) { | ||
return Object.values(o).reduce((e, t) => e + t, 0); | ||
} | ||
function h(o, t) { | ||
const e = o.headers.get("content-length") || "", n = parseInt(e, 10) || 5242880; | ||
function s(r, a) { | ||
t( | ||
function u(o, e) { | ||
const t = o.headers.get("content-length") || "", s = parseInt(t, 10) || 5242880; | ||
function r(i, n) { | ||
e( | ||
new CustomEvent("progress", { | ||
detail: { | ||
loaded: r, | ||
total: a | ||
loaded: i, | ||
total: n | ||
} | ||
@@ -101,19 +101,19 @@ }) | ||
new ReadableStream({ | ||
async start(r) { | ||
async start(i) { | ||
if (!o.body) { | ||
r.close(); | ||
i.close(); | ||
return; | ||
} | ||
const a = o.body.getReader(); | ||
let i = 0; | ||
const n = o.body.getReader(); | ||
let a = 0; | ||
for (; ; ) | ||
try { | ||
const { done: l, value: d } = await a.read(); | ||
if (d && (i += d.byteLength), l) { | ||
s(i, i), r.close(); | ||
const { done: l, value: h } = await n.read(); | ||
if (h && (a += h.byteLength), l) { | ||
r(a, a), i.close(); | ||
break; | ||
} else | ||
s(i, n), r.enqueue(d); | ||
r(a, s), i.enqueue(h); | ||
} catch (l) { | ||
console.error({ e: l }), r.error(l); | ||
console.error({ e: l }), i.error(l); | ||
break; | ||
@@ -130,3 +130,3 @@ } | ||
} | ||
class g extends EventTarget { | ||
class f extends EventTarget { | ||
constructor() { | ||
@@ -137,26 +137,26 @@ super(...arguments), this.#e = {}, this.#t = 0, this.progress = 0, this.mode = "REAL_TIME", this.caption = ""; | ||
#t; | ||
partialObserver(t, e = "") { | ||
const n = ++this.#t; | ||
return this.#e[n] = 0, (s) => { | ||
const { loaded: r, total: a } = s?.detail || {}; | ||
this.#e[n] = r / a * t, this.#s(this.totalProgress, "REAL_TIME", e); | ||
partialObserver(e, t = "") { | ||
const s = ++this.#t; | ||
return this.#e[s] = 0, (r) => { | ||
const { loaded: i, total: n } = r?.detail || {}; | ||
this.#e[s] = i / n * e, this.#s(this.totalProgress, "REAL_TIME", t); | ||
}; | ||
} | ||
slowlyIncrementBy(t) { | ||
const e = ++this.#t; | ||
this.#e[e] = t, this.#s(this.totalProgress, "SLOWLY_INCREMENT"); | ||
slowlyIncrementBy(e) { | ||
const t = ++this.#t; | ||
this.#e[t] = e, this.#s(this.totalProgress, "SLOWLY_INCREMENT"); | ||
} | ||
get totalProgress() { | ||
return Object.values(this.#e).reduce( | ||
(t, e) => t + e, | ||
(e, t) => e + t, | ||
0 | ||
); | ||
} | ||
#s(t, e, n) { | ||
#s(e, t, s) { | ||
this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
progress: t, | ||
mode: e, | ||
caption: n | ||
progress: e, | ||
mode: t, | ||
caption: s | ||
} | ||
@@ -167,6 +167,167 @@ }) | ||
} | ||
class g extends EventTarget { | ||
constructor({ | ||
weight: e = 1, | ||
caption: t = "", | ||
fillTime: s = 4 | ||
} = {}) { | ||
super(), this._selfWeight = 1, this._selfProgress = 0, this._selfCaption = "", this._isFilling = !1, this._subTrackers = [], this._weight = e, this._selfCaption = t, this._fillTime = s; | ||
} | ||
/** | ||
* Creates a new sub-tracker with a specific weight. | ||
* | ||
* The weight determines what percentage of the overall progress | ||
* the sub-tracker represents. For example, if the main tracker is | ||
* monitoring a process that has two stages, and the first stage | ||
* is expected to take twice as long as the second stage, you could | ||
* create the first sub-tracker with a weight of 0.67 and the second | ||
* sub-tracker with a weight of 0.33. | ||
* | ||
* The caption is an optional string that describes the current stage | ||
* of the operation. If provided, it will be used as the progress caption | ||
* for the sub-tracker. If not provided, the main tracker will look for | ||
* the next sub-tracker with a non-empty caption and use that as the progress | ||
* caption instead. | ||
* | ||
* Returns the newly-created sub-tracker. | ||
* | ||
* @throws {Error} If the weight of the new stage would cause the total weight of all stages to exceed 1. | ||
* | ||
* @param weight The weight of the new stage, as a decimal value between 0 and 1. | ||
* @param caption The caption for the new stage, which will be used as the progress caption for the sub-tracker. | ||
* | ||
* @example | ||
* ```ts | ||
* const tracker = new ProgressTracker(); | ||
* const subTracker1 = tracker.stage(0.67, 'Slow stage'); | ||
* const subTracker2 = tracker.stage(0.33, 'Fast stage'); | ||
* | ||
* subTracker2.set(50); | ||
* subTracker1.set(75); | ||
* subTracker2.set(100); | ||
* subTracker1.set(100); | ||
* ``` | ||
*/ | ||
stage(e, t = "") { | ||
if (e || (e = this._selfWeight), this._selfWeight - e < -Number.EPSILON) | ||
throw new Error( | ||
`Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.` | ||
); | ||
this._selfWeight -= e; | ||
const s = new g({ | ||
caption: t, | ||
weight: e, | ||
fillTime: this._fillTime | ||
}); | ||
return this._subTrackers.push(s), s.addEventListener("progress", () => this.notifyProgress()), s.addEventListener("done", () => { | ||
this.done && this.notifyDone(); | ||
}), s; | ||
} | ||
/** | ||
* Fills the progress bar slowly over time, simulating progress. | ||
* | ||
* The progress bar is filled in a 100 steps, and each step, the progress | ||
* is increased by 1. If `stopBeforeFinishing` is true, the progress bar | ||
* will stop filling when it reaches 99% so that you can call `finish()` | ||
* explicitly. | ||
* | ||
* If the progress bar is filling or already filled, this method does nothing. | ||
* | ||
* @example | ||
* ```ts | ||
* const progress = new ProgressTracker({ caption: 'Processing...' }); | ||
* progress.fillSlowly(); | ||
* ``` | ||
* | ||
* @param options Optional options. | ||
*/ | ||
fillSlowly({ stopBeforeFinishing: e = !0 } = {}) { | ||
if (this._isFilling) | ||
return; | ||
this._isFilling = !0; | ||
const t = 100, s = this._fillTime / t; | ||
this._fillInterval = setInterval(() => { | ||
this.set(this._selfProgress + 1), e && this._selfProgress >= 99 && clearInterval(this._fillInterval); | ||
}, s); | ||
} | ||
set(e) { | ||
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + Number.EPSILON >= 100 && this.finish(); | ||
} | ||
finish() { | ||
this._fillInterval && clearInterval(this._fillInterval), this._selfProgress = 100, this._isFilling = !1, this._fillInterval = void 0, this.notifyProgress(), this.notifyDone(); | ||
} | ||
get caption() { | ||
for (let e = this._subTrackers.length - 1; e >= 0; e--) | ||
if (!this._subTrackers[e].done) { | ||
const t = this._subTrackers[e].caption; | ||
if (t) | ||
return t; | ||
} | ||
return this._selfCaption; | ||
} | ||
setCaption(e) { | ||
this._selfCaption = e, this.notifyProgress(); | ||
} | ||
get done() { | ||
return this.progress + Number.EPSILON >= 100; | ||
} | ||
get progress() { | ||
return this._subTrackers.reduce( | ||
(e, t) => e + t.progress * t.weight, | ||
this._selfProgress * this._selfWeight | ||
); | ||
} | ||
get weight() { | ||
return this._weight; | ||
} | ||
get observer() { | ||
return this._progressObserver || (this._progressObserver = (e) => { | ||
this.set(e); | ||
}), this._progressObserver; | ||
} | ||
get loadingListener() { | ||
return this._loadingListener || (this._loadingListener = (e) => { | ||
this.set(e.detail.loaded / e.detail.total * 100); | ||
}), this._loadingListener; | ||
} | ||
pipe(e) { | ||
this.addEventListener("progress", (t) => { | ||
e.setProgress({ | ||
progress: t.detail.progress, | ||
caption: t.detail.caption | ||
}); | ||
}), this.addEventListener("done", () => { | ||
e.setLoaded(); | ||
}); | ||
} | ||
addEventListener(e, t) { | ||
super.addEventListener(e, t); | ||
} | ||
removeEventListener(e, t) { | ||
super.removeEventListener(e, t); | ||
} | ||
notifyProgress() { | ||
const e = this; | ||
this.dispatchEvent( | ||
new CustomEvent("progress", { | ||
detail: { | ||
get progress() { | ||
return e.progress; | ||
}, | ||
get caption() { | ||
return e.caption; | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
notifyDone() { | ||
this.dispatchEvent(new CustomEvent("done")); | ||
} | ||
} | ||
export { | ||
u as EmscriptenDownloadMonitor, | ||
g as ProgressObserver, | ||
h as cloneResponseMonitorProgress | ||
c as EmscriptenDownloadMonitor, | ||
f as ProgressObserver, | ||
g as ProgressTracker, | ||
u as cloneResponseMonitorProgress | ||
}; |
{ | ||
"name": "@php-wasm/progress", | ||
"version": "0.1.18", | ||
"version": "0.1.26", | ||
"description": "PHP.wasm – loading progress monitoring", | ||
@@ -32,4 +32,4 @@ "repository": { | ||
"types": "index.d.ts", | ||
"gitHead": "2de8f9182b249b7e87694efaa06e7f25b64b7bb3", | ||
"gitHead": "db8919156cbc8c4836ad93e1c821c677e2f7e4bd", | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
24317
7
596
1