()
| 19 | export const defaultScheduler: ScheduleFunction = systemSetTimeoutZero |
| 20 | |
| 21 | export function createNotifyManager() { |
| 22 | let queue: Array<NotifyCallback> = [] |
| 23 | let transactions = 0 |
| 24 | let notifyFn: NotifyFunction = (callback) => { |
| 25 | callback() |
| 26 | } |
| 27 | let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => { |
| 28 | callback() |
| 29 | } |
| 30 | let scheduleFn = defaultScheduler |
| 31 | |
| 32 | const schedule = (callback: NotifyCallback): void => { |
| 33 | if (transactions) { |
| 34 | queue.push(callback) |
| 35 | } else { |
| 36 | scheduleFn(() => { |
| 37 | notifyFn(callback) |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | const flush = (): void => { |
| 42 | const originalQueue = queue |
| 43 | queue = [] |
| 44 | if (originalQueue.length) { |
| 45 | scheduleFn(() => { |
| 46 | batchNotifyFn(() => { |
| 47 | originalQueue.forEach((callback) => { |
| 48 | notifyFn(callback) |
| 49 | }) |
| 50 | }) |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return { |
| 56 | /** |
| 57 | * Batches all updates scheduled inside the passed callback. |
| 58 | * This is mainly used internally to optimize query client updating. |
| 59 | * Batches can be nested; the queue is only flushed once the outermost `batch` call finishes. |
| 60 | * The return value of `callback` is passed through. |
| 61 | */ |
| 62 | batch: <T>(callback: () => T): T => { |
| 63 | let result |
| 64 | transactions++ |
| 65 | try { |
| 66 | result = callback() |
| 67 | } finally { |
| 68 | transactions-- |
| 69 | if (!transactions) { |
| 70 | flush() |
| 71 | } |
| 72 | } |
| 73 | return result |
| 74 | }, |
| 75 | /** |
| 76 | * All calls to the wrapped function will be batched. |
| 77 | */ |
| 78 | batchCalls: <T extends Array<unknown>>( |
no test coverage detected