@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
58 lines • 1.72 kB
TypeScript
/**
* Check if BroadcastChannel is supported
*/
export declare function isBroadcastChannelSupported(): boolean;
/**
* Create a reactive BroadcastChannel for cross-tab communication
*
* @example
* ```ts
* const channel = useBroadcastChannel('my-channel')
*
* // Subscribe to state changes
* channel.subscribe((state) => {
* console.log('Last message:', state.lastMessage)
* })
*
* // Post a message to all tabs
* channel.post({ type: 'sync', data: { user: 'John' } })
*
* // Close when done
* channel.close()
* ```
*/
export declare function useBroadcastChannel<T = unknown>(channelName: string, options?: BroadcastChannelOptions): BroadcastChannelRef<T>;
/**
* Simple one-shot broadcast to all tabs
*
* @example
* ```ts
* broadcast('app-events', { type: 'logout' })
* ```
*/
export declare function broadcast<T = unknown>(channelName: string, message: T): void;
/**
* Broadcast Channel Composables
*
* Reactive utilities for cross-tab/window communication using the BroadcastChannel API.
*/
export declare interface BroadcastChannelState<T = unknown> {
isSupported: boolean
isClosed: boolean
channel: BroadcastChannel | null
lastMessage: T | null
error: Error | null
}
export declare interface BroadcastChannelOptions {
onMessage?: (event: MessageEvent) => void
onError?: (event: MessageEvent) => void
}
export declare interface BroadcastChannelRef<T = unknown> {
get: () => BroadcastChannelState<T>
subscribe: (fn: (state: BroadcastChannelState<T>) => void) => () => void
post: (message: T) => void
close: () => void
isSupported: () => boolean
}
declare type BroadcastEventType = 'message' | 'error'
declare type BroadcastEventCallback = (data: MessageEvent) => void