UNPKG

reactant-share

Version:

A framework for building shared web applications with Reactant

26 lines (23 loc) 758 B
import { BroadcastChannel } from 'broadcast-channel'; import { createTransport, Transport } from 'data-transport'; export const createBroadcastTransport = (name: string, verbose?: boolean) => { const broadcastChannel = new BroadcastChannel( `reactant-share-channel:${name}` ); const transport = createTransport('Base', { listener: (callback) => { broadcastChannel.onmessage = (data) => { callback(JSON.parse(data)); }; return () => { broadcastChannel.onmessage = null; return broadcastChannel.close(); }; }, sender: (message) => broadcastChannel.postMessage(JSON.stringify(message)), prefix: `reactant-share:${name}`, verbose, }); return transport; }; export { Transport };