📄 useBroadcastChannel¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 3 |
| 📦 Imports | 9 |
| 📐 Interfaces | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useBroadcastChannel/index.ts
📦 Imports¶
| Name | Source |
|---|---|
ShallowRef |
vue |
ConfigurableWindow |
../_configurable |
Supportable |
../types |
tryOnMounted |
@vueuse/shared |
tryOnScopeDispose |
@vueuse/shared |
shallowRef |
vue |
defaultWindow |
../_configurable |
useEventListener |
../useEventListener |
useSupported |
../useSupported |
Functions¶
useBroadcastChannel(options: UseBroadcastChannelOptions): UseBroadcastChannelReturn<D, P>¶
Reactive BroadcastChannel
Parameters:
optionsany: No description
See: https://vueuse.org/useBroadcastChannel, https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel
Raw JSDoc
Calls:
useSupported (from ../useSupported)shallowRef (from vue)channel.value.postMessagechannel.value.closetryOnMounted (from @vueuse/shared)useEventListener (from ../useEventListener)tryOnScopeDispose (from @vueuse/shared)close
Code
export function useBroadcastChannel<D, P>(options: UseBroadcastChannelOptions): UseBroadcastChannelReturn<D, P> {
const {
name,
window = defaultWindow,
} = options
const isSupported = useSupported(() => window && 'BroadcastChannel' in window)
const isClosed = shallowRef(false)
const channel = shallowRef<BroadcastChannel | undefined>()
const data = shallowRef()
const error = shallowRef<Event | null>(null)
const post = (data: unknown) => {
if (channel.value)
channel.value.postMessage(data)
}
const close = () => {
if (channel.value)
channel.value.close()
isClosed.value = true
}
if (isSupported.value) {
tryOnMounted(() => {
error.value = null
channel.value = new BroadcastChannel(name)
const listenerOptions = {
passive: true,
}
useEventListener(channel, 'message', (e: MessageEvent) => {
data.value = e.data
}, listenerOptions)
useEventListener(channel, 'messageerror', (e: MessageEvent) => {
error.value = e
}, listenerOptions)
useEventListener(channel, 'close', () => {
isClosed.value = true
}, listenerOptions)
})
}
tryOnScopeDispose(() => {
close()
})
return {
isSupported,
channel,
data,
post,
close,
error,
isClosed,
}
}
Internal helpers¶
Declared inside another function in this file.
post(data: unknown): void¶
Parameters:
dataunknown
Returns: void
Calls:
channel.value.postMessage
close(): void¶
Returns: void
Calls:
channel.value.close
Interfaces¶
UseBroadcastChannelOptions¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string |
✗ | not shown |
UseBroadcastChannelReturn<D, P>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
channel |
ShallowRef<BroadcastChannel \| undefined> |
✗ | not shown |
data |
ShallowRef<D> |
✗ | not shown |
post |
(data: P) => void |
✗ | not shown |
close |
() => void |
✗ | not shown |
error |
ShallowRef<Event \| null> |
✗ | not shown |
isClosed |
ShallowRef<boolean> |
✗ | not shown |
Generated by Syntax Scribe