Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
86196c3feb | |||
0985ff145e | |||
7cd717fc7d | |||
2fe8cdb96a |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.7.2",
|
"version": "0.9.0",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -1,10 +1,3 @@
|
|||||||
import {TypedEmitter, TypedEvents} from './emitter';
|
|
||||||
|
|
||||||
export type downloadEvents = TypedEvents & {
|
|
||||||
complete: (blob: Blob) => any;
|
|
||||||
progress: (progress: number) => any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function download(href: any, name: string) {
|
export function download(href: any, name: string) {
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = href;
|
a.href = href;
|
||||||
@ -13,40 +6,3 @@ export function download(href: any, name: string) {
|
|||||||
a.click();
|
a.click();
|
||||||
document.body.removeChild(a);
|
document.body.removeChild(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Download a URL using fetch so progress can be tracked. Uses Typed Emitter to emit a "progress" &
|
|
||||||
* "complete" event.
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {string} downloadName
|
|
||||||
* @return {TypedEmitter<downloadEvents>}
|
|
||||||
*/
|
|
||||||
export function downloadProgress(url: string, downloadName?: string) {
|
|
||||||
const emitter = new TypedEmitter<downloadEvents>();
|
|
||||||
fetch(url).then(response => {
|
|
||||||
const contentLength = response.headers.get('Content-Length') || '0';
|
|
||||||
const total = parseInt(contentLength, 10);
|
|
||||||
let chunks: any[] = [], loaded = 0;
|
|
||||||
const reader = response.body?.getReader();
|
|
||||||
reader?.read().then(function processResult(result) {
|
|
||||||
if(result.done) {
|
|
||||||
const blob = new Blob(chunks);
|
|
||||||
if(downloadName) {
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
download(url, downloadName);
|
|
||||||
URL.revokeObjectURL(url);
|
|
||||||
}
|
|
||||||
emitter.emit('complete', blob);
|
|
||||||
} else {
|
|
||||||
const chunk = result.value;
|
|
||||||
chunks.push(chunk);
|
|
||||||
loaded += chunk.length;
|
|
||||||
const progress = loaded / total;
|
|
||||||
emitter.emit('progress', progress);
|
|
||||||
reader.read().then(processResult);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return emitter;
|
|
||||||
}
|
|
||||||
|
@ -6,6 +6,7 @@ export * from './errors';
|
|||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './misc';
|
export * from './misc';
|
||||||
export * from './objects';
|
export * from './objects';
|
||||||
|
export * from './promise-progress';
|
||||||
export * from './string';
|
export * from './string';
|
||||||
export * from './time';
|
export * from './time';
|
||||||
export * from './xhr';
|
export * from './xhr';
|
||||||
|
26
src/promise-progress.ts
Normal file
26
src/promise-progress.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
export type ProgressCallback = (progress: number) => any;
|
||||||
|
|
||||||
|
export class PromiseProgress<T> extends Promise<T> {
|
||||||
|
private listeners: ProgressCallback[] = [];
|
||||||
|
|
||||||
|
private _progress = 0;
|
||||||
|
get progress() { return this._progress; }
|
||||||
|
set progress(p: number) {
|
||||||
|
if(p == this._progress) return;
|
||||||
|
this._progress = p;
|
||||||
|
this.listeners.forEach(l => l(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(executor: (resolve: (value: T) => any, reject: (reason: any) => void, progress: (progress: number) => any) => void) {
|
||||||
|
super((resolve, reject) => executor(
|
||||||
|
(value: T) => resolve(value),
|
||||||
|
(reason: any) => reject(reason),
|
||||||
|
(progress: number) => this.progress = progress
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
onProgress(callback: ProgressCallback) {
|
||||||
|
this.listeners.push(callback);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,3 @@
|
|||||||
import {TypedEmitter, TypedEvents} from './emitter.ts';
|
|
||||||
import {clean} from './objects';
|
import {clean} from './objects';
|
||||||
|
|
||||||
export type Interceptor = (request: Response, next: () => void) => void;
|
export type Interceptor = (request: Response, next: () => void) => void;
|
||||||
|
Reference in New Issue
Block a user