diff --git a/.changeset/wrap-default-scheduler-queue-microtask.md b/.changeset/wrap-default-scheduler-queue-microtask.md new file mode 100644 index 00000000000..501ae1ef711 --- /dev/null +++ b/.changeset/wrap-default-scheduler-queue-microtask.md @@ -0,0 +1,5 @@ +--- +'@tanstack/query-core': patch +--- + +Wrap `queueMicrotask` in `defaultScheduler` so calling it as a method does not throw `TypeError: Illegal invocation` and spies on `globalThis.queueMicrotask` intercept calls. diff --git a/packages/query-core/src/__tests__/notifyManager.test.tsx b/packages/query-core/src/__tests__/notifyManager.test.tsx index 46037beacee..1bcfdfb1012 100644 --- a/packages/query-core/src/__tests__/notifyManager.test.tsx +++ b/packages/query-core/src/__tests__/notifyManager.test.tsx @@ -1,6 +1,10 @@ import { sleep } from '@tanstack/query-test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { createNotifyManager } from '../notifyManager' +import { + createNotifyManager, + defaultScheduler, + notifyManager, +} from '../notifyManager' describe('notifyManager', () => { beforeEach(() => { @@ -107,4 +111,50 @@ describe('notifyManager', () => { await vi.advanceTimersByTimeAsync(0) expect(callbackSpy).toHaveBeenCalledWith(1, 'test') }) + + it('should allow invoking defaultScheduler as a method without Illegal invocation', async () => { + const originalQueueMicrotask = globalThis.queueMicrotask + vi.spyOn(globalThis, 'queueMicrotask').mockImplementation(function ( + this: unknown, + callback: () => void, + ) { + if (this !== undefined && this !== globalThis) { + throw new TypeError('Illegal invocation') + } + originalQueueMicrotask(callback) + }) + + vi.resetModules() + const notifyManagerModule = await import('../notifyManager') + const callbackSpy = vi.fn<() => void>() + + notifyManagerModule.defaultScheduler(callbackSpy) + + expect(callbackSpy).not.toHaveBeenCalled() + await Promise.resolve() + expect(callbackSpy).toHaveBeenCalledTimes(1) + expect(callbackSpy).toHaveBeenCalledWith() + }) + + it('should delegate defaultScheduler and notifyManager to globalThis.queueMicrotask', async () => { + const queueMicrotaskSpy = vi.spyOn(globalThis, 'queueMicrotask') + const defaultSchedulerCallbackSpy = vi.fn<() => void>() + const notifyManagerCallbackSpy = vi.fn<() => void>() + + defaultScheduler(defaultSchedulerCallbackSpy) + notifyManager.schedule(notifyManagerCallbackSpy) + + expect(queueMicrotaskSpy).toHaveBeenCalledTimes(2) + expect(queueMicrotaskSpy).toHaveBeenNthCalledWith( + 1, + defaultSchedulerCallbackSpy, + ) + expect(queueMicrotaskSpy).toHaveBeenNthCalledWith(2, expect.any(Function)) + + await Promise.resolve() + expect(defaultSchedulerCallbackSpy).toHaveBeenCalledTimes(1) + expect(defaultSchedulerCallbackSpy).toHaveBeenCalledWith() + expect(notifyManagerCallbackSpy).toHaveBeenCalledTimes(1) + expect(notifyManagerCallbackSpy).toHaveBeenCalledWith() + }) }) diff --git a/packages/query-core/src/notifyManager.ts b/packages/query-core/src/notifyManager.ts index 1155f77841f..35a78aa5974 100644 --- a/packages/query-core/src/notifyManager.ts +++ b/packages/query-core/src/notifyManager.ts @@ -11,8 +11,11 @@ type ScheduleFunction = (callback: () => void) => void /** * Default scheduling function used by the notify manager. * Schedules the callback in a microtask with `queueMicrotask`. + * @param callback - The function to schedule in a microtask. */ -export const defaultScheduler: ScheduleFunction = queueMicrotask +export const defaultScheduler: ScheduleFunction = (callback) => { + queueMicrotask(callback) +} /** * Creates a notify manager, which schedules and batches notifications to subscribers. Query uses a