Skip to content
Merged
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
95 changes: 42 additions & 53 deletions packages/query-core/src/notifyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ export function createNotifyManager() {
}
let scheduleFn: ScheduleFunction = (cb) => setTimeout(cb, 0)

const setScheduler = (fn: ScheduleFunction) => {
scheduleFn = fn
}

const batch = <T>(callback: () => T): T => {
let result
transactions++
try {
result = callback()
} finally {
transactions--
if (!transactions) {
flush()
}
}
return result
}

const schedule = (callback: NotifyCallback): void => {
if (transactions) {
queue.push(callback)
Expand All @@ -48,20 +30,6 @@ export function createNotifyManager() {
})
}
}

/**
* All calls to the wrapped function will be batched.
*/
const batchCalls = <T extends Array<unknown>>(
callback: BatchCallsCallback<T>,
): BatchCallsCallback<T> => {
return (...args) => {
schedule(() => {
callback(...args)
})
}
}

const flush = (): void => {
const originalQueue = queue
queue = []
Expand All @@ -76,29 +44,50 @@ export function createNotifyManager() {
}
}

/**
* Use this method to set a custom notify function.
* This can be used to for example wrap notifications with `React.act` while running tests.
*/
const setNotifyFunction = (fn: NotifyFunction) => {
notifyFn = fn
}

/**
* Use this method to set a custom function to batch notifications together into a single tick.
* By default React Query will use the batch function provided by ReactDOM or React Native.
*/
const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {
batchNotifyFn = fn
}

return {
batch,
batchCalls,
batch: <T>(callback: () => T): T => {
let result
transactions++
try {
result = callback()
} finally {
transactions--
if (!transactions) {
flush()
}
}
return result
},
/**
* All calls to the wrapped function will be batched.
*/
batchCalls: <T extends Array<unknown>>(
callback: BatchCallsCallback<T>,
): BatchCallsCallback<T> => {
return (...args) => {
schedule(() => {
callback(...args)
})
}
},
schedule,
setNotifyFunction,
setBatchNotifyFunction,
setScheduler,
/**
* Use this method to set a custom notify function.
* This can be used to for example wrap notifications with `React.act` while running tests.
*/
setNotifyFunction: (fn: NotifyFunction) => {
notifyFn = fn
},
/**
* Use this method to set a custom function to batch notifications together into a single tick.
* By default React Query will use the batch function provided by ReactDOM or React Native.
*/
setBatchNotifyFunction: (fn: BatchNotifyFunction) => {
batchNotifyFn = fn
},
setScheduler: (fn: ScheduleFunction) => {
scheduleFn = fn
},
} as const
}

Expand Down