@php-wasm/progress 0.1.30 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.js +11 -10
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=5*1024*1024;class p extends EventTarget{#e={};#t={};constructor(e=[]){super(),this.setModules(e),this.#s()}getEmscriptenOptions(){return{dataFileDownloads:this.#i()}}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 t},{}),this.#t=Object.fromEntries(Object.entries(this.#e).map(([t])=>[t,0]))}#s(){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 e(n,...s)}}#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}}),!0}})}#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)}}))}}function c(o){return Object.values(o).reduce((e,t)=>e+t,0)}function u(o,e){const t=o.headers.get("content-length")||"",s=parseInt(t,10)||f;function r(i,n){e(new CustomEvent("progress",{detail:{loaded:i,total:n}}))}return new Response(new ReadableStream({async start(i){if(!o.body){i.close();return}const n=o.body.getReader();let a=0;for(;;)try{const{done:l,value:
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=5*1024*1024;class p extends EventTarget{#e={};#t={};constructor(e=[]){super(),this.setModules(e),this.#s()}getEmscriptenOptions(){return{dataFileDownloads:this.#i()}}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 t},{}),this.#t=Object.fromEntries(Object.entries(this.#e).map(([t])=>[t,0]))}#s(){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 e(n,...s)}}#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}}),!0}})}#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)}}))}}function c(o){return Object.values(o).reduce((e,t)=>e+t,0)}function u(o,e){const t=o.headers.get("content-length")||"",s=parseInt(t,10)||f;function r(i,n){e(new CustomEvent("progress",{detail:{loaded:i,total:n}}))}return new Response(new ReadableStream({async start(i){if(!o.body){i.close();return}const n=o.body.getReader();let a=0;for(;;)try{const{done:l,value:h}=await n.read();if(h&&(a+=h.byteLength),l){r(a,a),i.close();break}else r(a,s),i.enqueue(h)}catch(l){console.error({e:l}),i.error(l);break}}}),{status:o.status,statusText:o.statusText,headers:o.headers})}class _ extends EventTarget{constructor(){super(...arguments),this.#e={},this.#t=0,this.progress=0,this.mode="REAL_TIME",this.caption=""}#e;#t;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(e){const t=++this.#t;this.#e[t]=e,this.#s(this.totalProgress,"SLOWLY_INCREMENT")}get totalProgress(){return Object.values(this.#e).reduce((e,t)=>e+t,0)}#s(e,t,s){this.dispatchEvent(new CustomEvent("progress",{detail:{progress:e,mode:t,caption:s}}))}}const d=1e-5;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}stage(e,t=""){if(e||(e=this._selfWeight),this._selfWeight-e<-d)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}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+d>=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+d>=100}get progress(){const e=this._subTrackers.reduce((t,s)=>t+s.progress*s.weight,this._selfProgress*this._selfWeight);return Math.round(e*1e4)/1e4}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"))}}exports.EmscriptenDownloadMonitor=p;exports.ProgressObserver=_;exports.ProgressTracker=g;exports.cloneResponseMonitorProgress=u;
|
package/index.js
CHANGED
|
@@ -103,12 +103,12 @@ function u(o, e) {
|
|
|
103
103
|
let a = 0;
|
|
104
104
|
for (; ; )
|
|
105
105
|
try {
|
|
106
|
-
const { done: l, value:
|
|
107
|
-
if (
|
|
106
|
+
const { done: l, value: h } = await n.read();
|
|
107
|
+
if (h && (a += h.byteLength), l) {
|
|
108
108
|
i(a, a), r.close();
|
|
109
109
|
break;
|
|
110
110
|
} else
|
|
111
|
-
i(a, s), r.enqueue(
|
|
111
|
+
i(a, s), r.enqueue(h);
|
|
112
112
|
} catch (l) {
|
|
113
113
|
console.error({ e: l }), r.error(l);
|
|
114
114
|
break;
|
|
@@ -157,7 +157,7 @@ class p extends EventTarget {
|
|
|
157
157
|
);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
const
|
|
160
|
+
const d = 1e-5;
|
|
161
161
|
class c extends EventTarget {
|
|
162
162
|
constructor({
|
|
163
163
|
weight: e = 1,
|
|
@@ -202,7 +202,7 @@ class c extends EventTarget {
|
|
|
202
202
|
* ```
|
|
203
203
|
*/
|
|
204
204
|
stage(e, t = "") {
|
|
205
|
-
if (e || (e = this._selfWeight), this._selfWeight - e < -
|
|
205
|
+
if (e || (e = this._selfWeight), this._selfWeight - e < -d)
|
|
206
206
|
throw new Error(
|
|
207
207
|
`Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.`
|
|
208
208
|
);
|
|
@@ -244,7 +244,7 @@ class c extends EventTarget {
|
|
|
244
244
|
}, s);
|
|
245
245
|
}
|
|
246
246
|
set(e) {
|
|
247
|
-
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress +
|
|
247
|
+
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + d >= 100 && this.finish();
|
|
248
248
|
}
|
|
249
249
|
finish() {
|
|
250
250
|
this._fillInterval && clearInterval(this._fillInterval), this._selfProgress = 100, this._isFilling = !1, this._fillInterval = void 0, this.notifyProgress(), this.notifyDone();
|
|
@@ -262,13 +262,14 @@ class c extends EventTarget {
|
|
|
262
262
|
this._selfCaption = e, this.notifyProgress();
|
|
263
263
|
}
|
|
264
264
|
get done() {
|
|
265
|
-
return this.progress +
|
|
265
|
+
return this.progress + d >= 100;
|
|
266
266
|
}
|
|
267
267
|
get progress() {
|
|
268
|
-
|
|
269
|
-
(
|
|
268
|
+
const e = this._subTrackers.reduce(
|
|
269
|
+
(t, s) => t + s.progress * s.weight,
|
|
270
270
|
this._selfProgress * this._selfWeight
|
|
271
|
-
)
|
|
271
|
+
);
|
|
272
|
+
return Math.round(e * 1e4) / 1e4;
|
|
272
273
|
}
|
|
273
274
|
get weight() {
|
|
274
275
|
return this._weight;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/progress",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.32",
|
|
4
4
|
"description": "PHP.wasm – loading progress monitoring",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
"type": "module",
|
|
30
30
|
"main": "index.js",
|
|
31
31
|
"types": "index.d.ts",
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "868bafca7e77ad31e30c1500a0d4da8b1c2314cc",
|
|
33
33
|
"dependencies": {}
|
|
34
34
|
}
|