Appearance
EventBus
Central publish/subscribe channel used by the viewer, services, and plugins. Accessible as viewer.eventBus.
Usage
ts
const id = viewer.eventBus.on("model:loaded", ({ model, fileSpec }) => {
console.log("loaded", fileSpec.url, model);
});
viewer.eventBus.off(id);
viewer.eventBus.once("viewer:ready", ({ viewer }) => {
// runs at most once
});
viewer.eventBus.emit("my-app:custom", { foo: 1 });Subscriptions return a string id. Pass that id to off to remove a single listener. Passing an event name to off removes that listener id; use removeAllListeners(event) to remove every listener on an event.
IEventBus
| Method | Returns | Description |
|---|---|---|
emit<T>(event: string, data: T) | void | Fire event synchronously. All handlers run in subscription order. |
on<T>(event: string, handler) | string | Subscribe. Returns a listener id. |
once<T>(event: string, handler) | string | Subscribe for a single delivery. Returns a listener id. |
off(listenerId: string) | void | Remove a single listener by id. |
removeAllListeners(event?: string) | void | Remove all listeners on event, or all listeners if event is omitted. |
ts
type EventHandler<T = any> = (data: T) => void;EventMap
EventMap in src/core/interfaces/IEventBus.ts is the source of truth for known event names and payloads. Common entries include:
viewer:initializing,viewer:ready,viewer:resize,viewer:disposing,viewer:disposed,viewer:errormodel:loading-started,model:loading-progress,model:loaded,model:loading-errorcamera:changed,camera:fly-completeselection:object-selected,selection:object-deselected,selection:cleared,selection:hover-changedmeasurement:created,measurement:removed,measurement:point-addedplugin:registered,plugin:loaded,plugin:unloaded,plugin:errorservice:registered,service:initialized,service:destroyed,service:error
Refer to EventMap for the full list and payload shapes.
Notes
- Handlers run synchronously. Throwing in a handler does not abort other handlers but is logged.
- Subscriptions made from a plugin's
initmust be torn down indestroy(calloffon the saved ids, orremoveAllListenersfor events the plugin owns). - Emitting an event that is not in
EventMapis allowed but discouraged. New events should be added toEventMapin the same change.