-
Notifications
You must be signed in to change notification settings - Fork 11
/
abstract_runtime.ts
62 lines (55 loc) · 1.75 KB
/
abstract_runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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<Events extends EventsRecord>
extends EventEmitter<Events>
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<C extends Collab & Parent>(
rootCallback: (init: InitToken) => C
): C {
const rootCollab = rootCallback(new InitToken("", this));
this.rootCollab = rootCollab;
return rootCollab;
}
idOf<C extends Collab<CollabEventsRecord>>(collab: C): CollabID<C> {
if (collab.runtime !== this) {
throw new Error("idOf called with Collab from different Runtime");
}
return this.rootCollab.idOf(collab);
}
fromID<C extends Collab<CollabEventsRecord>>(id: CollabID<C>): C | undefined {
return this.rootCollab.fromID(id);
}
abstract childSend(
child: Collab<CollabEventsRecord>,
messageStack: (Uint8Array | string)[],
metaRequests: MetaRequest[]
): void;
}