Events

Events

Sync Engine uses a typed event system. Context provides on for subscribing and dispatch for publishing.

On and Dispatch

ts
type On<O extends object> = <K extends keyof O>(
  key: K,
  callback: (payload: O[K]) => void,
) => () => void;

type Dispatch<O extends object> = <K extends keyof O>(
  ...[key, payload]: undefined extends O[K] ? [K] : [K, O[K]]
) => void;

on returns a cleanup callback. dispatch makes payloadless events (type undefined) optional — call ctx.dispatch('syncCanceled') without a second argument.

ts
import type { Events, On, Dispatch } from '@hesprs/sync-engine-sdk';

const on: On<Events> = ctx.on;
const on: Dispatch<Events> = ctx.dispatch;

const unsubscribe = on('syncTerminated', (reason) => {
  if (reason.result === 'failed') console.error(reason.error);
});

dispatch('logGeneral', 'Example module started.');
dispatch('syncCanceled');

unsubscribe();

On<O> and Dispatch<O> are generic function types that allow custom event maps. In ordinary module code, use ctx.on<Events> and ctx.dispatch<Events>; type inference supplies valid event keys and payloads.

Events Map

Events is a merged event map contributed by all internal modules. Every event key and its payload type:

EventPayload
logSyncstring sync log message
logGeneralstring general log message
errorSyncstring sync error log message
errorGeneralstring general error log message
moduleLoadedstring module name
moduleUnloadedstring module name
syncStarted{ isCancelled: Ref<boolean>; trigger: string }
remoteWalkProgressProgress
syncTerminatedSyncTerminateReason
requestConfirmDeleteArray<RemoveLocal> pending local-remove tasks
requestConfirmTasksArray<BaseTask>
syncCanceledundefined (no payload)
taskCompletedTaskInfo ({ name: TaskNames; key: string; prettyName: string; isDir: boolean })
taskFailedFailedTaskInfo (TaskInfo & { error: string })
executionStartedArray<BaseTask>
tasksConfirmedArray<BaseTask>
deleteConfirmed{ delete: Array<RemoveLocal>; reupload: Array<RemoveLocal> }

TIP

syncStarted.isCancelled is a SynthKernel Ref<boolean> — call it as isCancelled() to read, or subscribe with isCancelled.subscribe(...).

All content licensed under the CC BY 4.0 License.