Skip to main content

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 the events object exported from @janhq/core.

Importing Events

Core Functions

on()

Adds an observer for an event.
Parameters:
  • eventName - The name of the event to observe
  • handler - The handler function to call when the event is observed
Example:

off()

Removes an observer for an event.
Parameters:
  • eventName - The name of the event to stop observing
  • handler - The handler function to remove
Example:

emit()

Emits an event to all observers.
Parameters:
  • eventName - The name of the event to emit
  • object - The object to pass to the event callback
Example:

Available Events

The library provides several event enums for type-safe event handling.

ModelEvent

Events related to model lifecycle.
Usage:

MessageEvent

Events related to messages in threads.
Usage:

InferenceEvent

Events related to inference operations.
Usage:

AssistantEvent

Events related to assistants.
Usage:

AppConfigurationEventName

Events related to application configuration.
Usage:

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