Interface Sync1Interface

An implementation of an entity synchronizer. A synchronizer allows you to sync entities with other peers, possibly over the network, etc.

Different syncers can be used to sync an entity across different protocols or connection types.

interface Sync1Interface {
    sendUpdate(entityId: `leaf:${string}`, update: Uint8Array): void;
    subscribe(
        entityId: `leaf:${string}`,
        handleUpdate: (entityId: `leaf:${string}`, update: Uint8Array) => void,
    ): () => void;
    syncSnapshots(
        entityId: `leaf:${string}`,
        snapshot: Uint8Array,
    ): Promise<Uint8Array<ArrayBufferLike>[]>;
}

Methods

  • Send an update for a given entity. This must be called to send local changes to remote peers.

    Parameters

    • entityId: `leaf:${string}`
    • update: Uint8Array

    Returns void

  • Subscribe to updates for a given entity.

    Every time the sync interface has an update for the entity it will call handleUpdate, passing it the entity ID and the update.

    Parameters

    • entityId: `leaf:${string}`

      The entity ID to subscribe to.

    • handleUpdate: (entityId: `leaf:${string}`, update: Uint8Array) => void

      The handler function to be invoked when the sync interface has a new update.

    Returns () => void

    Returns a function that may be called to unsubscribe.

  • Get the latest snapshots of the given entity and provide our latest snapshot as well.

    This will be called once when an entity is first starting to sync to get the latest known state, and to update the remote with our latest state.

    TODO: This needs to be made more sophisticated so that the entire snapshot doesn't need to be shared over the network when partial updates can be synced. At the same time, we may just want to wait for Sedimentree.

    Parameters

    • entityId: `leaf:${string}`

      The entity ID to fetch snapshots for.

    • snapshot: Uint8Array

    Returns Promise<Uint8Array<ArrayBufferLike>[]>