Skip to content

Commit

Permalink
Pause should not wait for retry delay
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Mar 2, 2024
1 parent 9382870 commit 3e6060d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-chunk-upload-action",
"version": "6.0.0",
"version": "6.1.0",
"description": "Uploading large files with chunking using server action in Next.js",
"main": "dist/index.js",
"scripts": {
Expand Down
40 changes: 36 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
this._onPaused = options.onPaused;
this._onAborted = options.onAborted;
this._onStatusChange = options.onStatusChange;
this._statusChangedEventListeners = {};

if (this._onStatusChange) this._onStatusChange(undefined, this.status);
}
Expand Down Expand Up @@ -182,6 +183,8 @@ export class ChunkUploader<TMetadata extends Metadata> {
const oldValue = this._status;
if (oldValue === value) return;
this._status = value;
const listenerSet = this._statusChangedEventListeners[value];
if (listenerSet) listenerSet.forEach(listener => listener());
if (this._onStatusChange) this._onStatusChange(oldValue, value);
}
protected _position: number;
Expand Down Expand Up @@ -238,7 +241,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
break;
} catch (error) {
if (this.status === 'pausing') return false;
if (retry < this._retryDelays.length) await wait(this._retryDelays[retry]);
if (retry < this._retryDelays.length) await this._waitForRetry(this._retryDelays[retry]);
else {
this.status = 'error';
this._error = error;
Expand Down Expand Up @@ -307,8 +310,37 @@ export class ChunkUploader<TMetadata extends Metadata> {
throw new Error('onStatusChange must be a function');
}
}
}

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

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

setTimeout(handleResolve, ms);

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

protected _statusChangedEventListeners: Partial<Record<ChunkUploaderStatus, Set<() => void>>>;

protected _addStatusChangedEventListener(status: ChunkUploaderStatus, listener: () => void) {
const listenerSet = this._statusChangedEventListeners[status] || new Set();
if (!this._statusChangedEventListeners[status])
this._statusChangedEventListeners[status] = listenerSet;
listenerSet.add(listener);
}

protected _removeStatusChangedEventListener(status: ChunkUploaderStatus, listener: () => void) {
const listenerSet = this._statusChangedEventListeners[status] || new Set();
if (!this._statusChangedEventListeners[status])
this._statusChangedEventListeners[status] = listenerSet;
listenerSet.delete(listener);
}
}

0 comments on commit 3e6060d

Please sign in to comment.