Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/scheduled-throttle-reset-before-callback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/scheduled": patch
---

Fix `throttle` and `scheduleIdle` getting stuck after the callback throws. The "scheduled" flag was reset only after the callback returned, so one throw made every later call a no-op. A call made from inside the callback was also dropped for the same reason. `leadingAndTrailing` now also resets its state when the trailing call throws.
12 changes: 8 additions & 4 deletions packages/scheduled/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ export const throttle: ScheduleCallback = (callback, wait) => {
if (isThrottled) return;
isThrottled = true;
timeoutId = setTimeout(() => {
callback(...lastArgs);
// reset first, so a throwing callback or a call made from inside it doesn't leave the throttle stuck
isThrottled = false;
callback(...lastArgs);
}, wait);
};

Expand Down Expand Up @@ -124,8 +125,8 @@ export const scheduleIdle: ScheduleCallback = isServer
isDeferred = true;
id = requestIdleCallback(
() => {
callback(...lastArgs);
isDeferred = false;
callback(...lastArgs);
},
{ timeout: maxWait },
);
Expand Down Expand Up @@ -236,8 +237,11 @@ export function leadingAndTrailing<Args extends unknown[]>(
let state = State.Ready;

const scheduled = schedule((args: Args) => {
state === State.Trailing && callback(...args);
state = State.Ready;
try {
state === State.Trailing && callback(...args);
} finally {
state = State.Ready;
}
}, wait);

const fn: typeof callback = (...args) => {
Expand Down
61 changes: 61 additions & 0 deletions packages/scheduled/test/schedule-idle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import type { scheduleIdle as ScheduleIdle } from "../src/index.js";

vi.useFakeTimers();

let scheduleIdle: typeof ScheduleIdle;

beforeAll(async () => {
// requestIdleCallback is picked when the module loads, so stub it before importing
vi.stubGlobal("requestIdleCallback", (cb: () => void, opts?: { timeout?: number }) =>
setTimeout(cb, opts?.timeout ?? 1),
);
vi.stubGlobal("cancelIdleCallback", (id: ReturnType<typeof setTimeout>) => clearTimeout(id));
vi.resetModules();
({ scheduleIdle } = await import("../src/index.js"));
});

afterAll(() => {
vi.unstubAllGlobals();
vi.useRealTimers();
});

describe("scheduleIdle", () => {
test("calls the callback with the last args", () => {
const calls: number[] = [];
const trigger = scheduleIdle((current: number) => calls.push(current), 20);

trigger(1);
trigger(2);
vi.advanceTimersByTime(20);
expect(calls).toEqual([2]);
});

test("keeps working after the callback throws", () => {
const calls: number[] = [];
const trigger = scheduleIdle((current: number) => {
calls.push(current);
if (current === 1) throw new Error("boom");
}, 20);

trigger(1);
expect(() => vi.advanceTimersByTime(20)).toThrow("boom");

trigger(2);
vi.advanceTimersByTime(20);
expect(calls).toEqual([1, 2]);
});

test("a call made from inside the callback is scheduled", () => {
const calls: number[] = [];
const trigger = scheduleIdle((current: number) => {
calls.push(current);
if (current < 2) trigger(current + 1);
}, 20);

trigger(1);
vi.advanceTimersByTime(20);
vi.advanceTimersByTime(20);
expect(calls).toEqual([1, 2]);
});
});
52 changes: 52 additions & 0 deletions packages/scheduled/test/throttle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ describe("throttle", () => {
expect(val).toBe(0);
});

test("keeps working after the callback throws", () => {
const calls: number[] = [];
const trigger = throttle((current: number) => {
calls.push(current);
if (current === 1) throw new Error("boom");
}, 20);

trigger(1);
expect(() => vi.advanceTimersByTime(50)).toThrow("boom");

trigger(2);
vi.advanceTimersByTime(50);
expect(calls).toEqual([1, 2]);
});

test("a call made from inside the callback is scheduled", () => {
const calls: number[] = [];
const trigger = throttle((current: number) => {
calls.push(current);
if (current < 3) trigger(current + 1);
}, 20);

trigger(1);
vi.advanceTimersByTime(20);
expect(calls).toEqual([1]);
vi.advanceTimersByTime(20);
expect(calls).toEqual([1, 2]);
vi.advanceTimersByTime(20);
expect(calls).toEqual([1, 2, 3]);
});

test("autoclearing throttle", () => {
let val = 0;
createRoot(dispose => {
Expand Down Expand Up @@ -109,6 +140,27 @@ describe("leading throttle", () => {
});

describe("leadingAndTrailing throttle", () => {
test("keeps working after the trailing call throws", () => {
const calls: number[] = [];
const trigger = leadingAndTrailing(
throttle,
(current: number) => {
calls.push(current);
if (current === 2) throw new Error("boom");
},
20,
);

trigger(1);
trigger(2);
expect(calls).toEqual([1]);
expect(() => vi.advanceTimersByTime(50)).toThrow("boom");
expect(calls).toEqual([1, 2]);

trigger(3);
expect(calls).toEqual([1, 2, 3]);
});

test("setup and trigger throttle", () => {
let val = 0;
const trigger = leadingAndTrailing(throttle, (current: number) => (val = current), 20);
Expand Down
Loading