# All files / src/_hooks EventRegistry.ts

46.15% Statements 6/13
0% Branches 0/8
25% Functions 1/4
45.45% Lines 5/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    195x

    5x
    3x

    3x

    3x

     import React from 'react'

    export const useEventRegistry = () => {
      const registry = React.useRef<Record<string, Function[]>>({})

      const addListener = React.useCallback((e: string, cb: Function) => {
        const currentCbs = registry.current[e] ?? []
        currentCbs.push(cb)
        registry.current[e] = currentCbs

        return () => {
          const cbs = registry.current[e] ?? []
          registry.current[e] = cbs.filter((callback) => callback !== cb)
        }
      }, [])

      return { registry, addListener }
    }