Synclets logoSynclets

SyncletImplementations

The SyncletImplementations type collects optional async callback functions that customize Synclet behavior at key lifecycle and operational points.

{
  onStart?: () => Promise<void>;
  onStop?: () => Promise<void>;
  onSync?: (address: AnyAddress<Depth>) => Promise<void>;
  onSendMessage?: (message: Message, to?: string) => Promise<void>;
  onReceiveMessage?: (message: Message, from: string) => Promise<void>;
  onSetAtom?: (address: AtomAddress<Depth>) => Promise<void>;
  getSendContext?: (receivedContext?: Context) => Promise<Context>;
  canReceiveMessage?: (context: Context) => Promise<boolean>;
  canReadAtom?: (address: AtomAddress<Depth>, context: Context) => Promise<boolean>;
  canWriteAtom?: (address: AtomAddress<Depth>, atom: Atom, context: Context) => Promise<boolean>;
  canRemoveAtom?: (address: AtomAddress<Depth>, context: Context) => Promise<boolean>;
  filterChildIds?: (address: AnyParentAddress<Depth>, childIds: string[], context: Context) => Promise<string[]>;
  getNow?: () => number;
}
TypeDescription
onStart?() => Promise<void>

The onStart function is called when the synclet starts.

onStop?() => Promise<void>

The onStop function is called when the synclet stops.

onSync?(address: AnyAddress<Depth>) => Promise<void>

The onSync function is called before syncing the provided address.

onSendMessage?(message: Message, to?: string) => Promise<void>

The onSendMessage function is called when a message is sent.

onReceiveMessage?(message: Message, from: string) => Promise<void>

The onReceiveMessage function is called when a message is received.

onSetAtom?(address: AtomAddress<Depth>) => Promise<void>

The onSetAtom function is called after a local Atom is set.

getSendContext?(receivedContext?: Context) => Promise<Context>

The getSendContext callback can be implemented to generate or augment the Context metadata attached to outgoing sync messages. This allows you to include authentication tokens, user identifiers, or other contextual information that receiving Synclets can use for authorization or routing.

canReceiveMessage?(context: Context) => Promise<boolean>

The canReceiveMessage callback can be implemented to filter incoming messages based on their Context metadata. Return true to accept the message or false to reject it. This is useful for implementing authentication, authorization, or message routing logic.

canReadAtom?(address: AtomAddress<Depth>, context: Context) => Promise<boolean>

The canReadAtom callback can be implemented to control read access to individual Atoms based on their address and the request Context. Return true to allow the read or false to deny it. This enables fine-grained authorization policies for data access.

canWriteAtom?(address: AtomAddress<Depth>, atom: Atom, context: Context) => Promise<boolean>

The canWriteAtom callback can be implemented to control write access to individual Atoms based on their address, the new value, and the request Context. Return true to allow the write or false to deny it. This enables fine-grained authorization and validation policies for data mutations.

canRemoveAtom?(address: AtomAddress<Depth>, context: Context) => Promise<boolean>

The canRemoveAtom callback can be implemented to control delete access to individual Atoms based on their address and the request Context. Return true to allow the deletion or false to deny it. This enables fine-grained authorization policies for data removal.

filterChildIds?(address: AnyParentAddress<Depth>, childIds: string[], context: Context) => Promise<string[]>

The filterChildIds callback can be implemented to selectively hide child IDs from enumeration based on the parent address and request Context. Return a filtered array of child IDs that should be visible to the caller. This enables hiding portions of the data tree from unauthorized users.

getNow?() => number

The getNow callback can be implemented to provide a custom clock value for timestamp generation. This is primarily useful for deterministic testing where you need reproducible timestamps. If not provided, Date.now() is used.

These callbacks allow you to inject custom logic for lifecycle events (onStart, onStop), synchronization hooks (onSync, onSetAtom), message handling (onSendMessage, onReceiveMessage, getSendContext), and fine-grained access control (canReceiveMessage, canReadAtom, canWriteAtom, canRemoveAtom, filterChildIds).

All callbacks are optional. The Synclet will use sensible defaults when callbacks are not provided.

Since

v0.0.0