Skip to content

Commit

Permalink
fix(types): notifyManager method typedef (#5569)
Browse files Browse the repository at this point in the history
Co-authored-by: jbovone <[email protected]>
Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
3 people authored Jun 16, 2023
1 parent df37d35 commit 74a0322
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/query-core/src/notifyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type NotifyFunction = (callback: () => void) => void

type BatchNotifyFunction = (callback: () => void) => void

type BatchCallsCallback<T extends unknown[]> = (...args: T) => void

export function createNotifyManager() {
let queue: NotifyCallback[] = []
let transactions = 0
Expand Down Expand Up @@ -45,12 +47,14 @@ export function createNotifyManager() {
/**
* All calls to the wrapped function will be batched.
*/
const batchCalls = <T extends Function>(callback: T): T => {
return ((...args: any[]) => {
const batchCalls = <T extends unknown[]>(
callback: BatchCallsCallback<T>,
): BatchCallsCallback<T> => {
return (...args) => {
schedule(() => {
callback(...args)
})
}) as any
}
}

const flush = (): void => {
Expand Down
15 changes: 15 additions & 0 deletions packages/query-core/src/tests/notifyManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ describe('notifyManager', () => {

expect(notifySpy).toHaveBeenCalledTimes(1)
})

it('typedefs should catch proper signatures', async () => {
const notifyManagerTest = createNotifyManager()

// we define some fn with its signature:
const fn: (a: string, b: number) => string = (a, b) => a + b

//now somefn expect to be called with args [a: string, b: number]
const someFn = notifyManagerTest.batchCalls(fn)

someFn('im happy', 4)

//@ts-expect-error
someFn('im not happy', false)
})
})

0 comments on commit 74a0322

Please sign in to comment.