Web SDKv1.0.0

EventEmitter

The type behind on and off. There are two emitters, and subscribing to the wrong one is silent. Document lifecycle events are global and fire on Kernstra.events; everything else is per-viewer and fires on viewer.on(...). A handler registered on the wrong emitter never runs and never warns. Each emitter is typed by its own map — GlobalEventMap for Kernstra.events, ViewerEventMap for viewer.on — so subscribing to a viewer event on the global emitter, or the reverse, is a compile error rather than a handler that never runs. SDKEventMap is both together, for a payload typed by name (SDKEventMap['pageChanged']). Every event and its payload is listed under Events.

Kernstra.events offers on and off only (the exported GlobalEvents type): only the SDK emits on it. EventEmitter is exported because it is the type behind on and off, not as a channel to emit through; its emit and removeAllListeners are the SDK's own. A handler that throws is logged and does not stop the others. Kernstra.destroy() leaves what is subscribed to Kernstra.events in place: a handler registered at module level hears the next init()'s initialized without subscribing again, and Kernstra.events is the same emitter for the page's life. No member needs a license feature or a document.

classviewer.on() / Kernstra.events.on()

Usage

TypeScript
import { Kernstra } from '@kernstra/pdf-web-sdk';

// Document lifecycle: global.
Kernstra.events.on('documentLoaded', ({ pageCount }) => setPageCount(pageCount));

// Everything else: on the viewer.
viewer.on('pageChanged', ({ pageIndex }) => setCurrentPage(pageIndex));

Subscribing

onmethodsince 1.0.0
on<K extends keyof T>(event: K, handler: EventHandler<T[K]>, options?: SubscribeOptions): () => void
Subscribe to a typed event. on() returns the unsubscribe; Kernstra.events.on takes the same options as viewer.on.
ParameterTypeDescription
eventKAn event name of this emitter's map: GlobalEventMap on Kernstra.events, ViewerEventMap on a viewer.
handlerEventHandler<T[K]>Receives the typed payload. One that throws is logged and does not stop the others.
options?SubscribeOptions{ once?: boolean; signal?: AbortSignal }. once hears the next emit only; signal unsubscribes when the AbortSignal aborts, so one AbortController can end every subscription a component made (a signal already aborted subscribes nothing).
Returns () => void The unsubscribe.
TypeScript
const stop = viewer.on('pageChanged', ({ pageIndex }) => setCurrentPage(pageIndex));
stop();

const controller = new AbortController();
viewer.on('zoomChanged', ({ scale }) => setZoom(scale), { signal: controller.signal });
viewer.on('documentChanged', () => markDirty(), { signal: controller.signal });
controller.abort(); // both gone

Kernstra.events.on('initialized', ({ version }) => console.log(version), { once: true });
offmethodsince 1.0.0
off<K extends keyof T>(event: K, handler: EventHandler<T[K]>): void
Unsubscribe from an event. off(event, handler) with the same handler reference works as well as the unsubscribe on() returned.
ParameterTypeDescription
eventKThe event name.
handlerEventHandler<T[K]>The same handler reference that was passed to on().
Returns void
TypeScript
const onLoaded = (data: SDKEventMap['documentLoaded']) => setPageCount(data.pageCount);
Kernstra.events.on('documentLoaded', onLoaded);
Kernstra.events.off('documentLoaded', onLoaded);

Emitting

emitmethodsince 1.0.0
emit<K extends keyof T>(event: K, data: T[K]): void
The SDK's own: calls every handler of the event synchronously, in the order they subscribed; one that throws is logged and does not stop the others. Kernstra.events exposes on and off only.
ParameterTypeDescription
eventKThe event name.
dataT[K]The payload, as the emitter's map types it.
Returns void
removeAllListenersmethodsince 1.0.0
removeAllListeners(): void
The SDK's own: drops every handler of every event, as a viewer's destroy() does. Kernstra.events exposes on and off only, so a host cannot drop every other subscriber.
Returns void