Pulled ALL skills from 15 source repositories: - anthropics/skills: 16 (docs, design, MCP, testing) - obra/superpowers: 14 (TDD, debugging, agents, planning) - coreyhaines31/marketingskills: 25 (marketing, CRO, SEO, growth) - better-auth/skills: 5 (auth patterns) - vercel-labs/agent-skills: 5 (React, design, Vercel) - antfu/skills: 16 (Vue, Vite, Vitest, pnpm, Turborepo) - Plus 13 individual skills from various repos Mosaic Stack is not limited to coding — the Orchestrator and subagents serve coding, business, design, marketing, writing, logistics, analysis, and more. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
102 lines
2.3 KiB
Markdown
102 lines
2.3 KiB
Markdown
---
|
|
category: Utilities
|
|
---
|
|
|
|
# useEventBus
|
|
|
|
A basic event bus.
|
|
|
|
## Usage
|
|
|
|
```ts
|
|
import { useEventBus } from '@vueuse/core'
|
|
|
|
const bus = useEventBus<string>('news')
|
|
|
|
function listener(event: string) {
|
|
console.log(`news: ${event}`)
|
|
}
|
|
|
|
// listen to an event
|
|
const unsubscribe = bus.on(listener)
|
|
|
|
// fire an event
|
|
bus.emit('The Tokyo Olympics has begun')
|
|
|
|
// unregister the listener
|
|
unsubscribe()
|
|
// or
|
|
bus.off(listener)
|
|
|
|
// clearing all listeners
|
|
bus.reset()
|
|
```
|
|
|
|
Listeners registered inside of components `setup` will be unregistered automatically when the component gets unmounted.
|
|
|
|
## TypeScript
|
|
|
|
Using `EventBusKey` is the key to bind the event type to the key, similar to Vue's [`InjectionKey`](https://antfu.me/posts/typed-provide-and-inject-in-vue) util.
|
|
|
|
```ts
|
|
// fooKey.ts
|
|
import type { EventBusKey } from '@vueuse/core'
|
|
|
|
export const fooKey: EventBusKey<{ name: foo }> = Symbol('symbol-key')
|
|
```
|
|
|
|
```ts
|
|
import { useEventBus } from '@vueuse/core'
|
|
|
|
import { fooKey } from './fooKey'
|
|
|
|
const bus = useEventBus(fooKey)
|
|
|
|
bus.on((e) => {
|
|
// `e` will be `{ name: foo }`
|
|
})
|
|
```
|
|
|
|
## Type Declarations
|
|
|
|
```ts
|
|
export type EventBusListener<T = unknown, P = any> = (
|
|
event: T,
|
|
payload?: P,
|
|
) => void
|
|
export type EventBusEvents<T, P = any> = Set<EventBusListener<T, P>>
|
|
export interface EventBusKey<T> extends Symbol {}
|
|
export type EventBusIdentifier<T = unknown> = EventBusKey<T> | string | number
|
|
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
|
|
}
|
|
export declare function useEventBus<T = unknown, P = any>(
|
|
key: EventBusIdentifier<T>,
|
|
): UseEventBusReturn<T, P>
|
|
```
|