import { Collab, CollabEventsRecord, InitToken } from "./collab"; import { CollabID } from "./collab_id"; import { EventEmitter, EventsRecord } from "./event_emitter"; import { IRuntime } from "./iruntime"; import { Parent } from "./parent"; import { MetaRequest } from "./updates"; /** * Skeletal implementation of [[IRuntime]] that uses * a root [[Collab]]. */ export abstract class AbstractRuntime extends EventEmitter implements IRuntime { readonly isRuntime: true = true; /** * A unique ID for this * [replica](https://collabs.readthedocs.io/en/latest/advanced/updates.html#terminology) * (copy of a Collabs document). */ readonly replicaID: string; protected rootCollab!: Collab & Parent; /** * @param replicaID This replica's `replicaID`, usually * obtained from [[ReplicaIDs]]. Must not be `""`. */ constructor(replicaID: string) { super(); if (replicaID === "") { throw new Error('replicaID must not be ""'); } this.replicaID = replicaID; } protected setRootCollab( rootCallback: (init: InitToken) => C ): C { const rootCollab = rootCallback(new InitToken("", this)); this.rootCollab = rootCollab; return rootCollab; } idOf>(collab: C): CollabID { if (collab.runtime !== this) { throw new Error("idOf called with Collab from different Runtime"); } return this.rootCollab.idOf(collab); } fromID>(id: CollabID): C | undefined { return this.rootCollab.fromID(id); } abstract childSend( child: Collab, messageStack: (Uint8Array | string)[], metaRequests: MetaRequest[] ): void; }