{"version":3,"file":"onlineManager.cjs","names":["Subscribable"],"sources":["../../src/onlineManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\n\ntype Listener = (online: boolean) => void\ntype SetupFn = (setOnline: Listener) => (() => void) | undefined\n\n/**\n * The `OnlineManager` manages the online state within TanStack Query. It can\n * be used to change the default event listeners or to manually change the\n * online state.\n *\n * By default, the `onlineManager` assumes an active network connection, and\n * listens to the `online` and `offline` events on the `window` object to\n * detect changes.\n */\nexport class OnlineManager extends Subscribable<Listener> {\n  #online = true\n  #cleanup?: () => void\n\n  #setup: SetupFn\n\n  constructor() {\n    super()\n    this.#setup = (onOnline) => {\n      // addEventListener does not exist in React Native, but window does\n      // oxlint-disable-next-line typescript/no-unnecessary-condition\n      if (typeof window !== 'undefined' && window.addEventListener) {\n        const onlineListener = () => onOnline(true)\n        const offlineListener = () => onOnline(false)\n        // Listen to online\n        window.addEventListener('online', onlineListener, false)\n        window.addEventListener('offline', offlineListener, false)\n\n        return () => {\n          // Be sure to unsubscribe if a new handler is set\n          window.removeEventListener('online', onlineListener)\n          window.removeEventListener('offline', offlineListener)\n        }\n      }\n\n      return\n    }\n  }\n\n  protected override onSubscribe(): void {\n    if (!this.#cleanup) {\n      this.setEventListener(this.#setup)\n    }\n  }\n\n  protected override onUnsubscribe() {\n    if (!this.hasListeners()) {\n      this.#cleanup?.()\n      this.#cleanup = undefined\n    }\n  }\n\n  /**\n   * `setEventListener` can be used to set a custom event listener that will\n   * be used to determine the online state. The provided `setup` function\n   * receives a `setOnline` callback that should be called with a `boolean`\n   * whenever the online state changes.\n   * @param setup - Receives the `setOnline` callback, registers the event listener, and may return\n   * a cleanup function that is called when the listener is replaced or no longer needed.\n   * @example\n   * ```ts\n   * import NetInfo from '@react-native-community/netinfo'\n   * import { onlineManager } from '@tanstack/query-core'\n   *\n   * onlineManager.setEventListener((setOnline) => {\n   *   return NetInfo.addEventListener((state) => {\n   *     setOnline(!!state.isConnected)\n   *   })\n   * })\n   * ```\n   */\n  setEventListener(setup: SetupFn): void {\n    this.#setup = setup\n    this.#cleanup?.()\n    this.#cleanup = setup(this.setOnline.bind(this))\n  }\n\n  /**\n   * `setOnline` can be used to manually set the online state.\n   * @param online - The online state.\n   * @example\n   * ```ts\n   * import { onlineManager } from '@tanstack/query-core'\n   *\n   * // Set to online\n   * onlineManager.setOnline(true)\n   *\n   * // Set to offline\n   * onlineManager.setOnline(false)\n   * ```\n   */\n  setOnline(online: boolean): void {\n    const changed = this.#online !== online\n\n    if (changed) {\n      this.#online = online\n      this.listeners.forEach((listener) => {\n        listener(online)\n      })\n    }\n  }\n\n  /**\n   * `isOnline` can be used to get the current online state.\n   * @returns `true` if online.\n   */\n  isOnline(): boolean {\n    return this.#online\n  }\n}\n\n/**\n * Singleton instance of {@link OnlineManager}, used to manage and observe the online state within TanStack Query.\n */\nexport const onlineManager = new OnlineManager()\n"],"mappings":";;;;;;;;;;;;AAcA,IAAa,gBAAb,cAAmCA,qBAAAA,aAAuB;CACxD,UAAU;CACV;CAEA;CAEA,cAAc;EACZ,MAAM;EACN,KAAK,UAAU,aAAa;GAG1B,IAAI,OAAO,WAAW,eAAe,OAAO,kBAAkB;IAC5D,MAAM,uBAAuB,SAAS,IAAI;IAC1C,MAAM,wBAAwB,SAAS,KAAK;IAE5C,OAAO,iBAAiB,UAAU,gBAAgB,KAAK;IACvD,OAAO,iBAAiB,WAAW,iBAAiB,KAAK;IAEzD,aAAa;KAEX,OAAO,oBAAoB,UAAU,cAAc;KACnD,OAAO,oBAAoB,WAAW,eAAe;IACvD;GACF;EAGF;CACF;CAEA,cAAuC;EACrC,IAAI,CAAC,KAAK,UACR,KAAK,iBAAiB,KAAK,MAAM;CAErC;CAEA,gBAAmC;EACjC,IAAI,CAAC,KAAK,aAAa,GAAG;GACxB,KAAK,WAAW;GAChB,KAAK,WAAW,KAAA;EAClB;CACF;;;;;;;;;;;;;;;;;;;;CAqBA,iBAAiB,OAAsB;EACrC,KAAK,SAAS;EACd,KAAK,WAAW;EAChB,KAAK,WAAW,MAAM,KAAK,UAAU,KAAK,IAAI,CAAC;CACjD;;;;;;;;;;;;;;;CAgBA,UAAU,QAAuB;EAG/B,IAFgB,KAAK,YAAY,QAEpB;GACX,KAAK,UAAU;GACf,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,MAAM;GACjB,CAAC;EACH;CACF;;;;;CAMA,WAAoB;EAClB,OAAO,KAAK;CACd;AACF;;;;AAKA,MAAa,gBAAgB,IAAI,cAAc"}