0.0.14 • Published 6 months ago

ot-engine v0.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

ot-engine

NPM version NPM downloads Build Status

Operational transformation engine

example

https://ot-engine-rich-text.herokuapp.com/

features

  • typescript full stack
  • support local undo/redo
  • support presence(cursors)

ot type definition

export type OTType<S, P,Pr> = {
  name: string;
  create?(data: any): S;
  applyAndInvert?(snapshot: S, op: P, invert: boolean): [S, P | undefined];
  apply?(snapshot: S, op: P): S;
  invert?(op: P): P;
  invertWithDoc?(op: P, snapshot: S): P;
  transform(op: P, refOp: P, side: OTSide): P;
  transformPresence?(presence: Pr, refOp: P, isOwnOp:boolean): Pr;
  serialize?(s: S): any;
  deserialize?(data: any): S;
};

ot-engine/server

export interface Op<P> {
  version: number;
  id: string;
  content: P;
}

export interface Snapshot<S> {
  version: number;
  rollback?: boolean;
  content: S;
}

export type SnapshotAndOps<S, P> = {
  snapshot: Snapshot<S>;
  ops: Op<P>[];
};

export interface GetOpsParams {
  custom?: any;
  collection: string;
  docId: string;
  fromVersion: number;
  toVersion?: number;
}

export interface GetSnapshotParams {
  custom?: any;
  collection: string;
  docId: string;
  version?: number;
  toVersion?: number;
}

export interface CommitOpParams<P> {
  custom?: any;
  collection: string;
  docId: string;
  op: Op<P>;
}

export interface SaveSnapshotParams<S> {
  custom?: any;
  collection: string;
  docId: string;
  snapshot: Snapshot<S>;
}

export interface DB {
    getOps<P>(params: GetOpsParams): Promise<Op<P>[]>;
    getSnapshot<S, P>(params: GetSnapshotParams): Promise<SnapshotAndOps<S, P> | undefined>;
    commitOp<P>(params: CommitOpParams<P>): Promise<void>;
    saveSnapshot<S>(params: SaveSnapshotParams<S>): Promise<void>;
}
export interface PubSubData {
    data: any;
}
export interface PubSub {
    subscribe(channel: string, callback: (d: PubSubData) => void): void;
    publish(channel: string, data: any): void;
    unsubscribe(channel: string, callback: (d: PubSubData) => void): void;
}
export interface ServerConfig {
    saveInterval?: number;
    db?: DB;
    pubSub?: PubSub;
}
export interface AgentConfig<S,P,Pr,Custom> {
  /** passed to db */
  custom?:Custom;
  stream: Duplex;
  collection: string;
  docId: string;
  clientId: string;
  otType: OTType<S,P,Pr>;
}
export declare class Server {
    constructor(params?: ServerConfig);
    handleStream<S,P,Pr,Custom>(config: AgentConfig<S,P,Pr,Custom>): void;
}

ot-engine/client

import { Event,EventTarget } from 'ts-event-target';

interface DocConfig<S,P,Pr> {
    socket: WebSocket;
    otType: OTType<S,P,Pr>;
    clientId: string;
    undoStackLimit?: number;
    cacheServerOpsLimit?: number;
}
declare class OpEvent<P> extends Event<'op'> {
    ops: P[];
    undoRedo: boolean;
    source: boolean;
    constructor();
}
declare class BeforeOpEvent<P> extends Event<'beforeOp'> {
    ops: P[];
    undoRedo: boolean;
    source: boolean;
    constructor();
}
declare class RemoteDeleteDocEvent extends Event<'remoteDeleteDoc'> {
  constructor();
}
declare class RollbackEvent extends Event<'rollback'> {
  constructor();
}
export declare class Doc<S,P,Pr> extends EventTarget<[
  OpEvent<P>,
  BeforeOpEvent<P>,
  RemoteDeleteDocEvent,
  RollbackEvent
  ]> {
    constructor(config: DocConfig<S,P,Pr>);
    canUndo(): boolean;
    canRedo(): boolean;
    undo(): void;
    redo(): void;
    destroy(): void;
    submitOp(opContent: P): void;
    submitPresence(presence: Pr): void;
    delete(): Promise<void>;
    rollback(params: {version:number}): Promise<void>;
    fetch(): Promise<Snapshot<S>>;
    data:S;
    remotePresences:Record<string,Pr>;
}

dev

npm i pnpm@8.x -g
pnpm i
pnpm dev

open: http://localhost:3000/

release workflow

npm run pub
git push heroku main:master --force
0.0.13

6 months ago

0.0.14

6 months ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.0

2 years ago