How to use the broadcast-channel.BroadcastChannel function in broadcast-channel

To help you get started, we’ve selected a few broadcast-channel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github unihooks / unihooks / src / useStore.js View on Github external
useSyncEffect(() => {
    const channel = channels[key] || (channels[key] = new BroadcastChannel(key))
    const notify = (value, ...args) => {
      return state.set(value)
    }
    channel.addEventListener('message', notify)
    channel.listenersCount = (channel.listenersCount || 0) + 1

    const id = store.watch(key, notify)
    return () => {
      store.unwatch(id)
      channel.removeEventListener('message', notify)
      channel.listenersCount--
      if (!channel.listenersCount) {
        delete channels[key]
        channel.close()
      }
    }

broadcast-channel

A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers, NodeJs, Deno and iframes

MIT
Latest version published 12 months ago

Package Health Score

79 / 100
Full package analysis