π $eventbus β Event Communication API β
The $eventbus enables decoupled communication between different parts of your plugin and the entire application.
It is based on a lightweight EventEmitter pattern.
π§ Core Concept β
Instead of directly calling functions across modules:
π emit events β listen β react
π§ Basic Usage β
ts
$eventbus.on("myEvent", (payload) => {
console.log(payload)
})
$eventbus.emit("myEvent", { foo: "bar" })π Global vs Local β
Plugin-local events β
ts
$eventbus.on(...)
$eventbus.emit(...)- scoped to your plugin
- no global side effects
Global events β
ts
$eventbus.global.on(...)
$eventbus.global.emit(...)- shared across all plugins
- automatically enriched with sender info
ts
$eventbus.global.emit("ui/addToSnackbar", {
message: "Hello",
})Payload becomes:
ts
{
sender: "<pluginKey>",
data: { message: "Hello" }
}π‘ API Methods β
on β
ts
const remove = $eventbus.on("event", callback)- registers listener
- returns cleanup function
emit β
ts
$eventbus.emit("event", payload)- triggers all listeners
once β
ts
$eventbus.once("event", callback)- triggers only once
- auto-removes listener
- timeout fallback (~5s)
removeListener β
ts
remove()- unregisters listener
π Typical Patterns β
UI communication β
ts
$eventbus.global.emit("ui/addToSnackbar", {
message: "Saved!",
color: "green",
})Cross-component sync β
ts
$eventbus.on("todoUpdated", () => {
execute()
})One-time event β
ts
$eventbus.once("initDone", () => {
console.log("Initialized")
})π§ Event Validation β
- Events can be restricted via whitelist
- Wildcards are supported
- Invalid events may throw errors
π§ Mental Model β
$eventbus= internal messaging system- decouples components and services
- avoids tight coupling
βοΈ When to Use $eventbus β
Use it for:
- UI interactions (snackbars, dialogs)
- cross-component communication
- plugin-wide events
- global app events
π« Important Notes β
- Events are not reactive
- No persistence (fire-and-forget)
- Overuse can make flow hard to trace
π§ Key Takeaway β
$eventbus enables flexible, decoupled communication
π Think: emit β listen β react