Add detailed download progress

This commit is contained in:
2023-07-22 21:08:54 +08:00
parent 1c1ddbf1ce
commit 26a0d045e0
4 changed files with 203 additions and 16 deletions

View File

@@ -0,0 +1,60 @@
type EventMap = {
"finished": number;
"progress": number;
};
export class ProgressReadable extends EventTarget {
readable: ReadableStream<Uint8Array>;
readed: number;
constructor(readable: ReadableStream<Uint8Array>) {
super();
this.readed = 0;
const reader = readable.getReader();
this.readable = new ReadableStream({
pull: async (c) => {
if (c.byobRequest) {
throw Error("Unimplemented.");
} else {
const v = await reader.read();
if (v.done) {
this.dispatchEvent("finished", this.readed);
c.close();
return;
} else {
this.readed += v.value.byteLength;
this.dispatchEvent("progress", this.readed);
c.enqueue(v.value);
}
}
},
cancel: (reason) => {
readable.cancel(reason);
},
type: "bytes",
});
}
// @ts-ignore Checked type
addEventListener<T extends keyof EventMap>(
type: T,
callback: (e: CustomEvent<EventMap[T]>) => void | Promise<void>,
options?: boolean | AddEventListenerOptions,
): void {
super.addEventListener(type, <EventListener> callback, options);
}
// @ts-ignore Checked type
dispatchEvent<T extends keyof EventMap>(type: T, detail: EventMap[T]) {
return super.dispatchEvent(new CustomEvent(type, { detail }));
}
// @ts-ignore Checked type
removeEventListener<T extends keyof EventMap>(
type: T,
callback: (e: CustomEvent<EventMap[T]>) => void | Promise<void>,
options?: boolean | EventListenerOptions,
): void {
super.removeEventListener(
type,
<EventListener> callback,
options,
);
}
}