Skip to content
Merged
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/wrap-default-scheduler-queue-microtask.md
Original file line number Diff line number Diff line change
@@ -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.
52 changes: 51 additions & 1 deletion packages/query-core/src/__tests__/notifyManager.test.tsx
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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()
})
})
5 changes: 4 additions & 1 deletion packages/query-core/src/notifyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading