Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get betterSetInterval ready for 2025 #496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/utilities/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,27 @@ export type BetterIntervalID = { _betterClearInterval: () => void };
* Similar to setInterval() but with two important differences:
* First, it assumes the function is asynchronous and only schedules its next invocation AFTER the asynchronous function finished.
* Second, it invokes the function immediately.
* @param func Asynchronous function to be periodically executed. It must take a single argument with a callback that
* the function must invoke after it's done.
* @param delay The number of milliseconds to wait to next invocation of the function.
* @param func Function to be periodically executed.
* For backwards compatibility reasons, it is passed a callback as its first argument during invocation, however that callback has no effect.
* @param delay The number of milliseconds to wait to next invocation of the function after the current invocation finishes.
* @returns Object that can be passed to betterClearInterval()
*/
export function betterSetInterval(func: (a: (...args: unknown[]) => unknown) => void, delay: number): BetterIntervalID {
let callback: (...a: unknown[]) => unknown;
let timeoutId: number;
export function betterSetInterval(func: ((a: (...args: unknown[]) => unknown) => void) | ((...args: unknown[]) => unknown), delay: number): BetterIntervalID {
let scheduleNextRun: () => void;
let timeoutId: NodeJS.Timeout;
let isRunning = true;

const funcWrapper = function () {
func(callback);
// Historically, the function was passed a callback that it needed to call to signal it was done.
// We keep passing this callback for backwards compatibility, but it has no effect anymore.
void new Promise((resolve) => resolve(func(() => undefined))).finally(scheduleNextRun);
};
callback = function () {
if (isRunning) timeoutId = setTimeout(funcWrapper, delay) as unknown as number;
scheduleNextRun = function () {
if (isRunning) timeoutId = setTimeout(funcWrapper, delay);
};
funcWrapper();
return {

return {
_betterClearInterval() {
isRunning = false;
clearTimeout(timeoutId);
Expand Down
59 changes: 59 additions & 0 deletions test/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,62 @@ describe('timeoutPromise()', () => {
});
});
});

describe('BetterSetInterval', () => {
it('works with normal function', async () => {
const fn = jest.fn();

const interval = utils.betterSetInterval(fn, 200);

// 3 x 200ms + some leeway
await utils.delayPromise(700);
utils.betterClearInterval(interval);

// 1st call is immediate, 3 more after 1, 2 and 3 intervals
expect(fn).toHaveBeenCalledTimes(4);

// No more calls after clearing the interval
await utils.delayPromise(500);
expect(fn).toHaveBeenCalledTimes(4);
});

it('works with async function', async () => {
const fn = jest.fn();

const interval = utils.betterSetInterval(async () => {
fn();
await utils.delayPromise(100);
}, 200);

// 3 x (200 + 100)ms + some leeway
await utils.delayPromise(1000);
utils.betterClearInterval(interval);

// 1st call is immediate, 3 more after 1, 2 and 3 intervals
expect(fn).toHaveBeenCalledTimes(4);

// No more calls after clearing the interval
await utils.delayPromise(500);
expect(fn).toHaveBeenCalledTimes(4);
});

it('works with function that accepts a callback (legacy)', async () => {
const fn = jest.fn();

const interval = utils.betterSetInterval((cb: () => void) => {
fn();
cb();
}, 200);

// 3 x 200ms + some leeway
await utils.delayPromise(700);
utils.betterClearInterval(interval);

// 1st call is immediate, 3 more after 1, 2 and 3 intervals
expect(fn).toHaveBeenCalledTimes(4);

// No more calls after clearing the interval
await utils.delayPromise(500);
expect(fn).toHaveBeenCalledTimes(4);
});
});
Loading