{"version":3,"file":"notifyManager.cjs","names":["systemSetTimeoutZero"],"sources":["../../src/notifyManager.ts"],"sourcesContent":["// TYPES\n\nimport { systemSetTimeoutZero } from './timeoutManager'\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\ntype BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void\n\ntype ScheduleFunction = (callback: () => void) => void\n\nexport const defaultScheduler: ScheduleFunction = systemSetTimeoutZero\n\nexport function createNotifyManager() {\n  let queue: Array<NotifyCallback> = []\n  let transactions = 0\n  let notifyFn: NotifyFunction = (callback) => {\n    callback()\n  }\n  let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n    callback()\n  }\n  let scheduleFn = defaultScheduler\n\n  const schedule = (callback: NotifyCallback): void => {\n    if (transactions) {\n      queue.push(callback)\n    } else {\n      scheduleFn(() => {\n        notifyFn(callback)\n      })\n    }\n  }\n  const flush = (): void => {\n    const originalQueue = queue\n    queue = []\n    if (originalQueue.length) {\n      scheduleFn(() => {\n        batchNotifyFn(() => {\n          originalQueue.forEach((callback) => {\n            notifyFn(callback)\n          })\n        })\n      })\n    }\n  }\n\n  return {\n    batch: <T>(callback: () => T): T => {\n      let result\n      transactions++\n      try {\n        result = callback()\n      } finally {\n        transactions--\n        if (!transactions) {\n          flush()\n        }\n      }\n      return result\n    },\n    /**\n     * All calls to the wrapped function will be batched.\n     */\n    batchCalls: <T extends Array<unknown>>(\n      callback: BatchCallsCallback<T>,\n    ): BatchCallsCallback<T> => {\n      return (...args) => {\n        schedule(() => {\n          callback(...args)\n        })\n      }\n    },\n    schedule,\n    /**\n     * Use this method to set a custom notify function.\n     * This can be used to for example wrap notifications with `React.act` while running tests.\n     */\n    setNotifyFunction: (fn: NotifyFunction) => {\n      notifyFn = fn\n    },\n    /**\n     * Use this method to set a custom function to batch notifications together into a single tick.\n     * By default React Query will use the batch function provided by ReactDOM or React Native.\n     */\n    setBatchNotifyFunction: (fn: BatchNotifyFunction) => {\n      batchNotifyFn = fn\n    },\n    setScheduler: (fn: ScheduleFunction) => {\n      scheduleFn = fn\n    },\n  } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"mappings":";;AAcA,MAAa,iDAAqCA,CAAAA,CAAAA;AAElD,SAAgB,sBAAsB;CACpC,IAAI,QAA+B,CAAC;CACpC,IAAI,eAAe;CACnB,IAAI,YAA4B,aAAa;EAC3C,SAAS;CACX;CACA,IAAI,iBAAsC,aAAyB;EACjE,SAAS;CACX;CACA,IAAI,aAAa;CAEjB,MAAM,YAAY,aAAmC;EACnD,IAAI,cACF,MAAM,KAAK,QAAQ;OAEnB,iBAAiB;GACf,SAAS,QAAQ;EACnB,CAAC;CAEL;CACA,MAAM,cAAoB;EACxB,MAAM,gBAAgB;EACtB,QAAQ,CAAC;EACT,IAAI,cAAc,QAChB,iBAAiB;GACf,oBAAoB;IAClB,cAAc,SAAS,aAAa;KAClC,SAAS,QAAQ;IACnB,CAAC;GACH,CAAC;EACH,CAAC;CAEL;CAEA,OAAO;EACL,QAAW,aAAyB;GAClC,IAAI;GACJ;GACA,IAAI;IACF,SAAS,SAAS;GACpB,UAAU;IACR;IACA,IAAI,CAAC,cACH,MAAM;GAEV;GACA,OAAO;EACT;;;;EAIA,aACE,aAC0B;GAC1B,QAAQ,GAAG,SAAS;IAClB,eAAe;KACb,SAAS,GAAG,IAAI;IAClB,CAAC;GACH;EACF;EACA;;;;;EAKA,oBAAoB,OAAuB;GACzC,WAAW;EACb;;;;;EAKA,yBAAyB,OAA4B;GACnD,gBAAgB;EAClB;EACA,eAAe,OAAyB;GACtC,aAAa;EACf;CACF;AACF;AAGA,MAAa,gBAAgB,oBAAoB"}