{"version":3,"file":"subscribable.cjs","names":[],"sources":["../../src/subscribable.ts"],"sourcesContent":["/**\n * The base class behind everything in Query that you can subscribe to: `QueryCache`, `MutationCache`,\n * the observers, and the `FocusManager`/`OnlineManager` behind `focusManager` and `onlineManager`.\n * Subclasses decide what a listener receives and when it is called.\n */\nexport class Subscribable<TListener extends Function> {\n  protected listeners = new Set<TListener>()\n\n  constructor() {\n    this.subscribe = this.subscribe.bind(this)\n  }\n\n  /**\n   * Registers a listener to be called on every update this object notifies about. Returns a function\n   * that removes the listener again — call it to stop listening. The base class never drops a listener\n   * on its own, though some subclasses clear all of theirs in `destroy()`.\n   * @param listener - Called on each update, with whatever the subclass passes to its subscribers.\n   * @returns A function that removes the listener.\n   * @example\n   * ```ts\n   * const unsubscribe = subscribable.subscribe(() => {\n   *   // react to the update\n   * })\n   *\n   * unsubscribe()\n   * ```\n   */\n  subscribe(listener: TListener): () => void {\n    this.listeners.add(listener)\n\n    this.onSubscribe()\n\n    return () => {\n      this.listeners.delete(listener)\n      this.onUnsubscribe()\n    }\n  }\n\n  /**\n   * Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.\n   * @returns `true` if at least one listener is registered.\n   */\n  hasListeners(): boolean {\n    return this.listeners.size > 0\n  }\n\n  /**\n   * Called after a listener is added. Does nothing here; subclasses override it, e.g. to start\n   * tracking what they observe once the first listener subscribes.\n   */\n  protected onSubscribe(): void {\n    // Do nothing\n  }\n\n  /**\n   * Called after a listener is removed. Does nothing here; subclasses override it, e.g. to clean up\n   * once the last listener unsubscribes.\n   */\n  protected onUnsubscribe(): void {\n    // Do nothing\n  }\n}\n"],"mappings":";;;;;;;AAKA,IAAa,eAAb,MAAsD;CAGpD,cAAc;EAFQ,KAAA,4BAAA,IAAI,IAAe;EAGvC,KAAK,YAAY,KAAK,UAAU,KAAK,IAAI;CAC3C;;;;;;;;;;;;;;;;CAiBA,UAAU,UAAiC;EACzC,KAAK,UAAU,IAAI,QAAQ;EAE3B,KAAK,YAAY;EAEjB,aAAa;GACX,KAAK,UAAU,OAAO,QAAQ;GAC9B,KAAK,cAAc;EACrB;CACF;;;;;CAMA,eAAwB;EACtB,OAAO,KAAK,UAAU,OAAO;CAC/B;;;;;CAMA,cAA8B,CAE9B;;;;;CAMA,gBAAgC,CAEhC;AACF"}