Skip to content

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

MethodReturnsDescription
emit<T>(event: string, data: T)voidFire event synchronously. All handlers run in subscription order.
on<T>(event: string, handler)stringSubscribe. Returns a listener id.
once<T>(event: string, handler)stringSubscribe for a single delivery. Returns a listener id.
off(listenerId: string)voidRemove a single listener by id.
removeAllListeners(event?: string)voidRemove 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:error
  • model:loading-started, model:loading-progress, model:loaded, model:loading-error
  • camera:changed, camera:fly-complete
  • selection:object-selected, selection:object-deselected, selection:cleared, selection:hover-changed
  • measurement:created, measurement:removed, measurement:point-added
  • plugin:registered, plugin:loaded, plugin:unloaded, plugin:error
  • service: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 init must be torn down in destroy (call off on the saved ids, or removeAllListeners for events the plugin owns).
  • Emitting an event that is not in EventMap is allowed but discouraged. New events should be added to EventMap in the same change.

XViewr SDK and xConvert CLI documentation.