UNPKG

broadcast-channel-state-sync

Version:

A TypeScript library for state synchronization across browser tabs using BroadcastChannel

112 lines (102 loc) 3.46 kB
import { Store, Slice } from '@reduxjs/toolkit'; import { StateTree, Store as Store$1 } from 'pinia'; import { StoreApi } from 'zustand'; interface StateManager<T> { getState: () => T; setState: (state: Partial<T>) => void; } interface BroadcastChannelOptions { channelName?: string; syncTimeout?: number; retryAttempts?: number; retryDelay?: number; instanceId?: string; } declare class BroadcastChannelManager<T> { private bc; private stateManager; private isReceivingBroadcast; private pendingSyncs; private options; private messageQueue; private processingQueue; constructor(stateManager: StateManager<T>, options?: BroadcastChannelOptions); private setupEventListeners; private handleMessage; private processMessageQueue; private handleStateRequest; private handleStateResponse; private handleStateUpdate; private handleSyncStart; private handleSyncEnd; private updateState; broadcastState(newState: Partial<T>): Promise<void>; private getInitialState; private requestState; private initializeState; destroy(): void; } interface BaseAdapterOptions<T> { getState: () => T; setState: (state: Partial<T>) => void; options?: BroadcastChannelOptions; } declare abstract class BaseAdapter<T> { protected readonly manager: BroadcastChannelManager<T>; constructor({ getState, setState, options }: BaseAdapterOptions<T>); protected broadcastState(newState: Partial<T>): Promise<void>; destroy(): void; } interface BroadcastAdapterOptions$3 { store: Store; slices: Record<string, Slice<any>>; options?: BroadcastChannelOptions; } declare class BroadcastAdapter$3<T> extends BaseAdapter<T> { private readonly slices; private readonly store; private unsubscribeCallback; private prevState; constructor({ store, slices, options }: BroadcastAdapterOptions$3); private setupSubscription; destroy(): void; } interface BroadcastAdapterOptions$2 { slices: Record<string, Store$1>; options?: BroadcastChannelOptions; } declare class BroadcastAdapter$2<T extends StateTree> extends BaseAdapter<T> { private readonly slices; private unsubscribeCallbacks; constructor({ slices, options }: BroadcastAdapterOptions$2); private setupSubscriptions; destroy(): void; } interface BroadcastAdapterOptions$1 { slices: Record<string, StoreApi<any>>; options?: BroadcastChannelOptions; } declare class BroadcastAdapter$1<T> extends BaseAdapter<T> { private readonly slices; private unsubscribeCallbacks; constructor({ slices, options }: BroadcastAdapterOptions$1); private setupSubscriptions; destroy(): void; } interface BroadcastAdapterOptions { store: any; stateKeys: string[]; options?: BroadcastChannelOptions; } declare class BroadcastAdapter<T> extends BaseAdapter<T> { private readonly store; private readonly stateKeys; private disposer; private prevState; private readonly getStateFn; constructor({ store, stateKeys, options }: BroadcastAdapterOptions); private setupSubscription; destroy(): void; } declare function generateUUID(): string; export { BroadcastChannelManager, type BroadcastChannelOptions, BroadcastAdapter as MobxAdapter, BroadcastAdapter$2 as PiniaAdapter, BroadcastAdapter$3 as ReduxAdapter, type StateManager, BroadcastAdapter$1 as ZustandAdapter, generateUUID };