Updated XHR to HTTP with PromiseProgress support

This commit is contained in:
2024-07-18 23:59:12 -04:00
parent 7b2d4ba119
commit 2a878b7962
5 changed files with 151 additions and 93 deletions

View File

@ -15,12 +15,38 @@ export class PromiseProgress<T> extends Promise<T> {
super((resolve, reject) => executor(
(value: T) => resolve(value),
(reason: any) => reject(reason),
(progress: number) => this.progress = progress
(progress: number) => this.progress = progress,
));
}
static from<T>(promise: Promise<T>): PromiseProgress<T> {
if(promise instanceof PromiseProgress) return promise;
return new PromiseProgress<T>((res, rej) => promise
.then((...args) => res(...args))
.catch((...args) => rej(...args)));
}
private from(promise: Promise<T>): PromiseProgress<T> {
const newPromise = PromiseProgress.from(promise);
this.onProgress(p => newPromise.progress = p);
return newPromise;
}
onProgress(callback: ProgressCallback) {
this.listeners.push(callback);
return this;
}
then(res?: (v: T) => any, rej?: (err: any) => any): PromiseProgress<any> {
const resp = super.then(res, rej);
return this.from(resp);
}
catch(rej?: (err: any) => any): PromiseProgress<any> {
return this.from(super.catch(rej));
}
finally(res?: () => any): PromiseProgress<any> {
return this.from(super.finally(res));
}
}