{"version":3,"sources":["../../src/use-event-bus/index.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { useLatest } from '../use-latest'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUnmount } from '../use-unmount'\n\nimport type { AnyFunc, Noop } from '../utils/basic'\n\nexport type EventBusListener<T = any, P = any> = (event: T, payload?: P) => void\nexport type EventBusEvents<T, P = any> = Set<EventBusListener<T, P>>\nexport type EventBusIdentifier = symbol | string | number\n\nexport interface UseEventBusReturns<T, P> {\n  /**\n   * Subscribe to an event. When calling emit, the listeners will execute.\n   *\n   * @param listener watch listener.\n   * @returns a stop function to remove the current callback.\n   */\n  on: (listener: EventBusListener<T, P>) => AnyFunc\n  /**\n   * Similar to `on`, but only fires once\n   *\n   * @param listener watch listener.\n   * @returns a stop function to remove the current callback.\n   */\n  once: (listener: EventBusListener<T, P>) => AnyFunc\n  /**\n   * Emit an event, the corresponding event listeners will execute.\n   *\n   * @param event data sent.\n   */\n  emit: (event?: T, payload?: P) => void\n  /**\n   * Remove the corresponding listener.\n   *\n   * @param listener watch listener.\n   */\n  off: (listener: EventBusListener<T>) => void\n  /**\n   * Clear all events in global scope\n   */\n  reset(): void\n  /**\n   * Clear all listeners in the current component context\n   */\n  cleanup(): void\n}\n\nexport interface UseEventBusOptions {\n  /**\n   * Whether to automatically clean up the listener when the component is unmounted.\n   *\n   * @defaultValue true\n   */\n  autoCleanup?: boolean\n}\n\nexport const events: Map<EventBusIdentifier, EventBusEvents<any>> = /* #__PURE__ */ new Map<\n  EventBusIdentifier,\n  EventBusEvents<any>\n>()\n\n/**\n * A React Hook that provides a simple event bus for your application.\n *\n * TODO: add a hooks to consume event (auto release when unmount)\n */\nexport function useEventBus<T = any, P = any>(\n  key: EventBusIdentifier,\n  options: UseEventBusOptions = {},\n): UseEventBusReturns<T, P> {\n  const { autoCleanup = true } = options\n\n  const cleanups = useRef<Noop[]>([])\n  const latest = useLatest({ key })\n\n  const on = useStableFn((listener: EventBusListener<T, P>) => {\n    const { key } = latest.current\n    const listeners = events.get(key) || new Set()\n    listeners.add(listener)\n    events.set(key, listeners)\n    const cleanup = () => off(listener)\n    cleanups.current.push(cleanup)\n    return cleanup\n  })\n\n  const once = useStableFn((listener: EventBusListener<T, P>) => {\n    function _listener(...args: Parameters<EventBusListener<T, P>>) {\n      off(_listener)\n      listener(...args)\n    }\n    on(_listener)\n    const cleanup = () => off(_listener)\n    cleanups.current.push(cleanup)\n    return cleanup\n  })\n\n  const off = useStableFn((listener: EventBusListener<T>): void => {\n    const listeners = events.get(latest.current.key)\n    if (!listeners) return\n    listeners.delete(listener)\n    if (!listeners.size) reset()\n  })\n\n  const emit = useStableFn((event?: T, payload?: P) => {\n    for (const notice of events.get(latest.current.key) ?? []) {\n      notice(event, payload)\n    }\n  })\n\n  // for global events reset\n  const reset = useStableFn(() => events.delete(latest.current.key))\n\n  // for current component auto unmount cleanup\n  const cleanup = useStableFn(() => {\n    for (const cleanup of cleanups.current) cleanup()\n    cleanups.current.length = 0\n  })\n\n  useUnmount(autoCleanup && cleanup)\n\n  return {\n    on,\n    once,\n    off,\n    emit,\n    reset,\n    cleanup,\n  }\n}\n"],"names":["events","useEventBus","Map","key","options","autoCleanup","cleanups","useRef","latest","useLatest","on","useStableFn","listener","current","listeners","get","Set","add","set","cleanup","off","push","once","_listener","args","delete","size","reset","emit","event","payload","notice","length","useUnmount"],"mappings":";;;;;;;;;;;QAyDaA;eAAAA;;QAUGC;eAAAA;;;qBAnEO;yBACG;2BACE;0BACD;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDpB,IAAMD,SAAuD,aAAa,GAAG,IAAIE;AAUjF,SAASD,YACdE,GAAuB;QACvBC,UAAAA,iEAA8B,CAAC;IAE/B,2BAA+BA,QAAvBC,aAAAA,gDAAc;IAEtB,IAAMC,WAAWC,IAAAA,aAAM,EAAS,EAAE;IAClC,IAAMC,SAASC,IAAAA,oBAAS,EAAC;QAAEN,KAAAA;IAAI;IAE/B,IAAMO,KAAKC,IAAAA,wBAAW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