Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function createChannel(id = APP_ID) {
return new BroadcastChannel(id);
}
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()
}
}