📄 useEventBus¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 8 |
| 📦 Imports | 3 |
| 📐 Interfaces | 2 |
| 📑 Type Aliases | 3 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useEventBus/index.ts
📦 Imports¶
| Name | Source |
|---|---|
Fn |
@vueuse/shared |
tryOnScopeDispose |
@vueuse/shared |
events |
./internal |
Functions¶
useEventBus(key: EventBusIdentifier<T>): UseEventBusReturn<T, P>¶
Parameters:
keyEventBusIdentifier<T>
Returns: UseEventBusReturn<T, P>
Calls:
events.getlisteners.addevents.setofftryOnScopeDispose (from @vueuse/shared)listeneronlisteners.deleteresetevents.deleteevents.get(key)?.forEachv
Internal Comments:
Code
export function useEventBus<T = unknown, P = any>(key: EventBusIdentifier<T>): UseEventBusReturn<T, P> {
function on(listener: EventBusListener<T, P>) {
const listeners = (events.get(key) || new Set())
listeners.add(listener)
events.set(key, listeners)
const _off = () => off(listener)
// auto unsubscribe when scope get disposed
tryOnScopeDispose(_off)
return _off
}
function once(listener: EventBusListener<T, P>) {
function _listener(...args: any[]) {
off(_listener)
// @ts-expect-error cast
listener(...args)
}
return on(_listener)
}
function off(listener: EventBusListener<T>): void {
const listeners = events.get(key)
if (!listeners)
return
listeners.delete(listener)
if (!listeners.size)
reset()
}
function reset() {
events.delete(key)
}
function emit(event?: T, payload?: P) {
events.get(key)?.forEach(v => v(event, payload))
}
return { on, once, off, emit, reset }
}
Internal helpers¶
Declared inside another function in this file.
on(listener: EventBusListener<T, P>): () => void¶
Parameters:
listenerEventBusListener<T, P>
Returns: () => void
Calls:
events.getlisteners.addevents.setofftryOnScopeDispose (from @vueuse/shared)
Internal Comments:
Code
_off(): void¶
Returns: void
Calls:
off
once(listener: EventBusListener<T, P>): () => void¶
Parameters:
listenerEventBusListener<T, P>
Returns: () => void
Calls:
offlisteneron
Internal Comments:
Code
_listener(args: any[]): void¶
Parameters:
argsany[]
Returns: void
Calls:
offlistener
Internal Comments:
Code
off(listener: EventBusListener<T>): void¶
Parameters:
listenerEventBusListener<T>
Returns: void
Calls:
events.getlisteners.deletereset
Code
reset(): void¶
Returns: void
Calls:
events.delete
emit(event: T, payload: P): void¶
Parameters:
eventTpayloadP
Returns: void
Calls:
events.get(key)?.forEachv
Interfaces¶
EventBusKey<T>¶
UseEventBusReturn<T, P>¶
Interface Code
export interface UseEventBusReturn<T, P> {
/**
* Subscribe to an event. When calling emit, the listeners will execute.
* @param listener watch listener.
* @returns a stop function to remove the current callback.
*/
on: (listener: EventBusListener<T, P>) => Fn
/**
* Similar to `on`, but only fires once
* @param listener watch listener.
* @returns a stop function to remove the current callback.
*/
once: (listener: EventBusListener<T, P>) => Fn
/**
* Emit an event, the corresponding event listeners will execute.
* @param event data sent.
*/
emit: (event?: T, payload?: P) => void
/**
* Remove the corresponding listener.
* @param listener watch listener.
*/
off: (listener: EventBusListener<T>) => void
/**
* Clear all events
*/
reset: () => void
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
on |
(listener: EventBusListener<T, P>) => Fn |
✗ | not shown |
once |
(listener: EventBusListener<T, P>) => Fn |
✗ | not shown |
emit |
(event?: T, payload?: P) => void |
✗ | not shown |
off |
(listener: EventBusListener<T>) => void |
✗ | not shown |
reset |
() => void |
✗ | not shown |
Type Aliases¶
EventBusListener<T = unknown, P = any>¶
EventBusEvents<T, P = any>¶
EventBusIdentifier<T = unknown>¶
Generated by Syntax Scribe