Skip to content

Commit

Permalink
fix(query-core): jsdoc of notifyManager (#8031)
Browse files Browse the repository at this point in the history
* fix(query-core): jsdoc of notifyManager

* chore: update

---------

Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
manudeli and TkDodo authored Sep 9, 2024
1 parent 4cc4bd2 commit 2c1be7b
Showing 1 changed file with 42 additions and 53 deletions.
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

0 comments on commit 2c1be7b

Please sign in to comment.