json-crdt-repo
Version:
JSON CRDT server and syncing local-first browser client
83 lines (82 loc) • 2.77 kB
TypeScript
import { type NodeBuilder } from 'json-joy/lib/json-crdt';
import type { BlockId, LocalRepo } from '../local/types';
import { EditSession } from './EditSession';
export interface EditSessionFactoryOpts {
readonly sid: number;
readonly repo: LocalRepo;
}
export declare class EditSessionFactory {
protected readonly opts: EditSessionFactoryOpts;
constructor(opts: EditSessionFactoryOpts);
/**
* Creates a new editing session synchronously (immediately). If the block
* with a given ID already exists, it asynchronously synchronizes the local
* and remote state.
*/
make(opts: EditSessionMakeOpts): {
session: EditSession;
sync?: Promise<void>;
};
/**
* Load block from the local repo. Creates a new editing session
* asynchronously from an existing local block.
*
* It is also possible to block on remote state check in case the block does
* not exist locally, or to pull the latest state from the remote.
*/
load(opts: EditSessionLoadOpts): Promise<EditSession>;
}
/**
* Constructs a new editing session synchronously.
*/
export interface EditSessionMakeOpts {
/** Block ID. */
id: BlockId;
/** Thew new block schema, if any. */
schema?: NodeBuilder;
/**
* Weather to asynchronously pull for any existing local block state, if a
* block with the same ID already exists. Defaults to `true`.
*/
pull?: boolean;
/**
* Internal unique session ID.
*/
session?: number;
}
/**
* Constructs and editing session asynchronously from an existing block. In
* case the block does not exist, it is possible to create one or throw an
* error.
*/
export interface EditSessionLoadOpts {
/** Block ID. */
id: BlockId;
/**
* If specified, will create a new block, if one does not already exist. Will
* use these `make` options and provide them to the `make()` call.
*/
make?: Omit<EditSessionMakeOpts, 'id'>;
/** Thew new block schema, if any. */
schema?: NodeBuilder;
/**
* Internal unique session ID.
*/
session?: number;
remote?: {
/**
* Time in milliseconds to wait for the remote to respond. If the remote
* does not respond in time, the call will proceed with the local state.
*
* If upsert `make` option is not provided, the call will throw a "TIMEOUT"
* error.
*/
timeout?: number;
/**
* Defaults to an empty string. Otherwise, if "missing", will throw a
* "NOT_FOUND" error if the block does not exist remotely. If "exists", will
* a "CONFLICT" error if the block exists remotely.
*/
throwIf?: '' | 'missing' | 'exists';
};
}