Register a listener for a specific event type
The listener function will be called whenever an event with the matching name is dispatched. The same listener can be registered multiple times, but should typically be registered only once per component instance.
Important: Always remove listeners in disconnectedCallback() to
prevent memory leaks.
eventDispatcher.addListener('message-received', (event) => {
console.log('Message:', event.detail);
});
Name of the event to listen for
Callback function to invoke when the event is dispatched
Dispatch a custom event to all registered listeners
Creates and dispatches a CustomEvent with the provided data. All components
that have registered listeners for this event name will be notified.
The CustomEvent that was dispatched
const eventDispatcher = platform.get(PlatformServiceName.EventDispatcher);
eventDispatcher.dispatch('item-selected', { itemId: 42, name: 'Widget' });
Unique name identifying the event type
Data payload to include in the event's detail property
Unregister a previously registered event listener
Removes the specified listener function for the given event name. The listener reference must be the same function instance that was passed to addListener.
Always call this in disconnectedCallback() to clean up listeners and
prevent memory leaks.
const handler = (event: CustomEvent<string>) => console.log(event.detail);
eventDispatcher.addListener('message-received', handler);
eventDispatcher.removeListener('message-received', handler);
Name of the event to stop listening for
The exact listener function that was previously registered
Service for application-wide event communication
The EventDispatcher enables loosely-coupled communication between components through a publish-subscribe pattern.
Components can dispatch custom events and subscribe to events from other components without direct coupling. This is particularly useful for:
Example
Basic usage