Event System
The@janhq/core event system enables decoupled communication between extensions and the core application. Extensions can listen for events, emit events, and react to changes throughout the application.
Event API
The event system is accessed through theevents object exported from @janhq/core.
Importing Events
Core Functions
on()
Adds an observer for an event.eventName- The name of the event to observehandler- The handler function to call when the event is observed
off()
Removes an observer for an event.eventName- The name of the event to stop observinghandler- The handler function to remove
emit()
Emits an event to all observers.eventName- The name of the event to emitobject- The object to pass to the event callback
Available Events
The library provides several event enums for type-safe event handling.ModelEvent
Events related to model lifecycle.MessageEvent
Events related to messages in threads.InferenceEvent
Events related to inference operations.AssistantEvent
Events related to assistants.AppConfigurationEventName
Events related to application configuration.Extension Event Patterns
Basic Event Listener
Emitting Custom Events
Event Chain Pattern
Best Practices
Use Type-Safe Events
Always use the provided event enums instead of string literals for type safety.
Clean Up Listeners
Remove event listeners in
onUnload() to prevent memory leaks.Avoid Blocking
Keep event handlers fast. Use async operations for heavy work.
Error Handling
Wrap event handlers in try-catch blocks to prevent crashes.
Clean Up Pattern
Error Handling
Implementation Details
The event system is implemented in~/workspace/source/core/src/browser/events.ts:
The event system uses the global
core object internally. Extensions should always use the exported events API.Next Steps
Extension Base Classes
Learn how to use events in extensions
Type Definitions
Explore event payload types