Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query-core): jsdoc of notifyManager #8031

Merged
merged 5 commits into from
Sep 9, 2024
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 @@
}
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 @@
})
}
}

/**
* 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 @@
}
}

/**
* 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

Check warning on line 86 in packages/query-core/src/notifyManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-core/src/notifyManager.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
},
setScheduler: (fn: ScheduleFunction) => {
scheduleFn = fn
},
} as const
}

Expand Down
Loading