Skip to main content

KSync Class

The KSync class is the main interface for creating and managing real-time synchronized data stores. It provides methods for event handling, storage management, and synchronization.

Constructor

new KSync(options?)

Creates a new KSync instance.
Parameters:
  • options (optional): Configuration object
    • storage: Storage adapter for persisting events
    • syncClient: Client for synchronizing with remote peers
    • mode: Synchronization mode (‘event-sourcing’ | ‘crdt’)

Properties

events

  • Type: Map<string, any>
  • Description: Read-only map of all events in the store
  • Example:

state

  • Type: any
  • Description: Current materialized state of the store
  • Example:

version

  • Type: number
  • Description: Current version/sequence number of the store
  • Example:

Core Methods

emit(eventType, data)

Emits a new event to the store.
Parameters:
  • eventType (string): Type of the event
  • data (any): Event payload data
Returns: Promise<void>

on(eventType, handler)

Registers an event listener.
Parameters:
  • eventType (string): Event type to listen for, or ’*’ for all events
  • handler (function): Event handler function
Returns: () => void (unsubscribe function)

off(eventType, handler)

Removes an event listener.
Parameters:
  • eventType (string): Event type
  • handler (function): Handler function to remove

materialize(materializer)

Sets up a materializer function to compute derived state.
Parameters:
  • materializer (function): Function that takes events and returns computed state

query(queryFn)

Queries the current state.
Parameters:
  • queryFn (function): Function that receives current state and returns filtered data
Returns: Query result

Synchronization Methods

sync()

Manually triggers synchronization with remote peers.
Returns: Promise<void>

connect()

Connects to the sync server (if using WebSocket sync).
Returns: Promise<void>

disconnect()

Disconnects from the sync server.
Returns: Promise<void>

CRDT Methods

When using CRDT mode, additional methods are available:

createLWWRegister(key, initialValue)

Creates a Last-Write-Wins register.

createGSet(key, initialItems?)

Creates a Grow-only Set.

createGCounter(key, initialValue?)

Creates a Grow-only Counter.

getCRDT(key)

Retrieves an existing CRDT by key.

Storage Methods

persist()

Manually triggers persistence to storage.
Returns: Promise<void>

load()

Loads events from storage.
Returns: Promise<void>

clear()

Clears all events and resets the store.
Returns: Promise<void>

Event Lifecycle

Event Object Structure

Every event in kSync has the following structure:

Event Flow

  1. Emit: emit() creates a new event
  2. Validate: Event is validated and assigned an ID/version
  3. Store: Event is added to the event store
  4. Materialize: State is recomputed using the materializer
  5. Notify: Event listeners are triggered
  6. Persist: Event is saved to storage (if configured)
  7. Sync: Event is sent to remote peers (if connected)

Error Handling

kSync methods can throw the following errors:

Complete Example