Skip to content

📄 $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