Skip to content

Latest commit

 

History

History
48 lines (32 loc) · 1.87 KB

File metadata and controls

48 lines (32 loc) · 1.87 KB

Home

Broadcast Channel Plugin

A plugin for @yobta/stores that allows them to receive state updates from other instances of the same store using the browser's BroadcastChannel API.

It is important to note that this plugin does not persist state, meaning that stores may have different initial states.

Updates are only received by the plugin when the store has subscribers.

Usage

import { createStore, broadcastChannelPlugin } from '@yobta/stores'

const store = createStore(
  { count: 0 },
  broadcastChannelPlugin({ channel: 'my-store' }),
)

Arguments

The plugin factory function accepts an object with the following properties:

  • channel (string) — The name of the channel.
  • codec (YobtaCodec, optional) The codec to use for serializing and deserializing the state. Defaults to jsonCodec.

Codec

The default codec provided by plugin is able to decode simple values such as numbers, strings, and booleans, as well as arrays and plain objects. However, for more complex data structures like maps and sets, a special codec may be required to properly serialize and deserialize the state. This can be done by specifying an codec function as an optional argument when creating the broadcastChannelPlugin instance. The codec function should be able to handle the serialization and deserialization of the desired data structure, allowing the store to properly synchronize via the broadcastChannel.

import { createMapStore, broadcastChannelPlugin, mapCodec } from '@yobta/stores'

const store = createMapStore(
  { key: 'value' },
  broadcastChannelPlugin({
    channel: 'my-map-store-yobta',
    codec: mapCodec,
  }),
)

See Also