-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Clear timeout when request completed
- Loading branch information
Showing
6 changed files
with
53 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Timeout } from './timeout'; | ||
|
||
it('creates timeout controller with specific limit', () => { | ||
jest.useFakeTimers(); | ||
const timeout = new Timeout(1000 * 3); | ||
|
||
const callback = jest.fn(); | ||
timeout.signal.addEventListener('abort', callback); | ||
|
||
jest.advanceTimersByTime(2900); | ||
expect(callback).not.toBeCalled(); | ||
jest.advanceTimersByTime(100); | ||
expect(callback).toBeCalled(); | ||
jest.clearAllTimers(); | ||
|
||
timeout.clear(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import { MastoTimeoutError } from '../errors'; | ||
import type { AbortSignal } from '@mastojs/ponyfills'; | ||
import { AbortController } from '@mastojs/ponyfills'; | ||
|
||
export const timeout = async <T>(task: Promise<T>, ms?: number): Promise<T> => { | ||
// It actually is depending on the runtime... | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let cancellationToken: any | undefined; | ||
export class Timeout { | ||
private readonly abortController: AbortController; | ||
private readonly timeout: NodeJS.Timeout; | ||
|
||
if (ms == undefined) { | ||
return task; | ||
constructor(millisecond: number) { | ||
this.abortController = new AbortController(); | ||
this.timeout = setTimeout(() => { | ||
this.abortController.abort(); | ||
}, millisecond); | ||
} | ||
|
||
const timeoutPromise = new Promise<never>((_, reject) => { | ||
cancellationToken = setTimeout( | ||
() => void reject(new MastoTimeoutError(`Timeout of ${ms}ms exceeded`)), | ||
ms, | ||
) as unknown as number; | ||
}); | ||
|
||
const mainPromise = task.then((value) => { | ||
clearTimeout(cancellationToken as NodeJS.Timeout); | ||
return value; | ||
}); | ||
get signal(): AbortSignal { | ||
return this.abortController.signal; | ||
} | ||
|
||
return Promise.race([timeoutPromise, mainPromise]); | ||
}; | ||
clear(): void { | ||
clearTimeout(this.timeout); | ||
} | ||
} |