forked from sergiodxa/remix-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
interface Options { | ||
signal?: AbortSignal; | ||
} | ||
|
||
/** | ||
* Wait for a specified amount of time, accepts a signal to abort the timer. | ||
* @param ms The amount of time to wait in milliseconds | ||
* @param options The options for the timer | ||
* @example | ||
* let controller = new AbortController(); | ||
* await wait(1000, { signal: controller.signal }); | ||
*/ | ||
export function wait(ms: number, options?: Options): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
let timeout = setTimeout(() => { | ||
if (options?.signal?.aborted) return reject(new TimersError("Aborted")); | ||
return resolve(); | ||
}, ms); | ||
if (options?.signal) { | ||
options.signal.addEventListener("abort", () => { | ||
clearTimeout(timeout); | ||
reject(new TimersError("Aborted")); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Get an async iterable that yields on an interval until aborted. | ||
* @param ms The amount of time to wait between intervals, in milliseconds | ||
* @param options The options for the timer | ||
* @returns An async iterable that yields on each intervals | ||
* @example | ||
* let controller = new AbortController(); | ||
* for await (let _ of interval(1000, { signal: controller.signal })) { | ||
* // Do something every second until aborted | ||
* } | ||
*/ | ||
export async function* interval(ms: number, options?: Options) { | ||
let signal = options?.signal ?? new AbortSignal(); | ||
while (!signal.aborted) { | ||
try { | ||
yield await wait(ms, { signal }); | ||
} catch { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
export class TimersError extends globalThis.Error {} |
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,47 @@ | ||
import { wait, interval, TimersError } from "../../src/common/timers"; | ||
import { describe, test, expect } from "vitest"; | ||
|
||
describe("Timers", () => { | ||
describe(wait.name, () => { | ||
test("should resolve after the specified time", async () => { | ||
let start = Date.now(); | ||
await wait(100); | ||
let end = Date.now(); | ||
expect(end - start).toBeGreaterThanOrEqual(100); | ||
}); | ||
|
||
test("should reject if aborted", async () => { | ||
let controller = new AbortController(); | ||
let start = Date.now(); | ||
let promise = wait(100, { signal: controller.signal }); | ||
controller.abort(); | ||
await expect(promise).rejects.toThrowError(TimersError); | ||
let end = Date.now(); | ||
expect(end - start).toBeLessThan(100); | ||
}); | ||
}); | ||
|
||
describe(interval.name, () => { | ||
test("should resolve after the specified time", async () => { | ||
let controller = new AbortController(); | ||
let start = Date.now(); | ||
let iterator = interval(100, { signal: controller.signal }); | ||
let next = await iterator.next(); | ||
let end = Date.now(); | ||
expect(end - start).toBeGreaterThanOrEqual(100); | ||
expect(next.done).toBe(false); | ||
controller.abort(); | ||
}); | ||
|
||
test("should reject if aborted", async () => { | ||
let controller = new AbortController(); | ||
let start = Date.now(); | ||
let iterator = interval(100, { signal: controller.signal }); | ||
controller.abort(); | ||
let next = await iterator.next(); | ||
let end = Date.now(); | ||
expect(end - start).toBeLessThan(100); | ||
expect(next.done).toBe(true); | ||
}); | ||
}); | ||
}); |