Skip to content

Commit

Permalink
allow aborting when status is 'error'
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Mar 2, 2024
1 parent d14f5c6 commit f2456e4
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ export class ChunkUploader<TMetadata extends Metadata> {

/**
* Abort the upload process.
* returns `false` if the status is not `paused`.
* returns `false` if the status is not `paused` or `error`.
*
* status: `paused` -> `aborted`
* status: `paused` or `error` -> `aborted`
*/
public abort() {
if (!this.canAbort) return false;
Expand All @@ -173,7 +173,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
return true;
}
public get canAbort() {
return this.status === 'paused';
return this.status === 'paused' || this.status === 'error';
}

/*************
Expand Down Expand Up @@ -239,8 +239,10 @@ export class ChunkUploader<TMetadata extends Metadata> {
break;
} catch (error) {
if (this.status === 'pausing') return false;
if (retry < this._retryDelays.length) await this._waitForRetry(this._retryDelays[retry]);
else {
if (retry < this._retryDelays.length) {
const isPausd = await this._waitForRetry(this._retryDelays[retry]);
if (isPausd) return false;
} else {
this.status = 'error';
this._error = error;
if (this._onError) this._onError(error);
Expand Down Expand Up @@ -308,19 +310,28 @@ export class ChunkUploader<TMetadata extends Metadata> {
}

protected _waitForRetry(ms: number) {
return new Promise<void>(resolve => {
return new Promise<boolean>(resolve => {
let isResolved = false;

const handleResolve = () => {
const handleTimeout = () => {
if (isResolved) return;
isResolved = true;
this._removeStatusChangedEventListener('pausing', handlePause);
clearTimeout(timeoutId);
resolve(false);
};

const handlePause = () => {
if (isResolved) return;
isResolved = true;
this._removeStatusChangedEventListener('pausing', handleResolve);
resolve();
this._removeStatusChangedEventListener('pausing', handlePause);
clearTimeout(timeoutId);
resolve(true);
};

setTimeout(handleResolve, ms);
const timeoutId = setTimeout(handleTimeout, ms);

this._addStatusChangedEventListener('pausing', handleResolve);
this._addStatusChangedEventListener('pausing', handlePause);
});
}

Expand Down

0 comments on commit f2456e4

Please sign in to comment.