-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlist-fugue-simple.ts
364 lines (311 loc) · 10.3 KB
/
list-fugue-simple.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// This is a port from the fugue repository Oct 2023
// Commit 98c0c7a965276fb9a22237562f642a6ce8d8e03f
interface ID {
sender: string;
counter: number;
}
// const idEq = (a: ID | null | undefined, b: ID | null): boolean => (
// a == b || (
// a != null && b != null
// && a.sender === b.sender && a.counter === b.counter
// )
// )
interface Element<T> {
/** For the start & end, this is ("", 0) & ("", 1). */
id: ID;
value: T | null;
isDeleted: boolean;
/** null for start and end. */
leftOrigin: Element<T> | null;
/** null for start and end. */
rightOrigin: Element<T> | null;
/** Linked list structure: the element currently to our left. */
left: Element<T> | null;
/** Linked list structure: the element currently to our right. */
right: Element<T> | null;
}
interface InsertMessage<T> {
type: "insert";
id: ID;
value: T;
leftOrigin: ID;
rightOrigin: ID;
}
interface DeleteMessage {
type: "delete";
id: ID;
}
type Message<T> = InsertMessage<T> | DeleteMessage
export class ListFugueSimple<T> {
readonly start: Element<T>;
readonly end: Element<T>;
counter = 0;
/**
* Used in getByID.
*
* Map from ID.sender, to an array that maps ID.counter, to element with that ID.
*/
readonly elementsByID = new Map<string, Element<T>[]>();
/** Cached length. */
_length = 0;
// All the elements we've seen, in causal order. This makes saving & loading
// much more simple.
msgsInCausalOrder: Message<T>[] = []
replicaId: string
constructor(replicaId: string) {
this.replicaId = replicaId
this.start = {
id: { sender: "", counter: 0 },
value: null,
isDeleted: true,
leftOrigin: null,
rightOrigin: null,
left: null,
right: null,
};
this.end = {
id: { sender: "", counter: 1 },
value: null,
isDeleted: true,
leftOrigin: null,
rightOrigin: null,
left: this.start,
right: null,
};
this.start.right = this.end;
this.elementsByID.set("", [this.start, this.end]);
}
insert(index: number, ...values: T[]): T | undefined {
for (let i = 0; i < values.length; i++) {
this.insertOne(index + i, values[i]);
}
// The return value is just an interface requirement; not relevant here.
return undefined;
}
private insertOne(index: number, value: T) {
// insert generator.
const id = { sender: this.replicaId, counter: this.counter };
this.counter++;
const leftOrigin = index === 0 ? this.start : this.getByIndex(index - 1);
const rightOrigin = leftOrigin.right!;
const msg: InsertMessage<T> = {
type: "insert",
id,
value,
leftOrigin: leftOrigin.id,
rightOrigin: rightOrigin.id,
};
// Message is delivered to receivePrimitive (the effector).
// super.sendPrimitive(JSON.stringify(msg));
this.receivePrimitive(msg)
}
delete(startIndex: number, count = 1): void {
for (let i = 0; i < count; i++) this.deleteOne(startIndex);
}
private deleteOne(index: number): void {
// delete generator.
const elt = this.getByIndex(index);
const msg: DeleteMessage = { type: "delete", id: elt.id };
// Message is delivered to receivePrimitive (the effector).
// super.sendPrimitive(JSON.stringify(msg));
this.receivePrimitive(msg)
}
protected receivePrimitive(msg: Message<T>): void {
// const msg: InsertMessage<T> | DeleteMessage = JSON.parse(<string>message);
switch (msg.type) {
case "insert": {
// insert effector
if (this.hasID(msg.id)) return // We already have this item.
const leftOrigin = this.getByID(msg.leftOrigin);
const rightOrigin = this.getByID(msg.rightOrigin);
const left = this.computeLeft(msg.id, leftOrigin, rightOrigin);
// Insert a new elt into the linked last after left.
const right = left.right!;
const elt: Element<T> = {
id: msg.id,
value: msg.value,
isDeleted: false,
leftOrigin,
rightOrigin,
left,
right,
};
left.right = elt;
right.left = elt;
// Add elt to elementsByID.
let bySender = this.elementsByID.get(msg.id.sender);
if (bySender === undefined) {
bySender = [];
this.elementsByID.set(msg.id.sender, bySender);
}
bySender[msg.id.counter] = elt;
this._length++;
// In a production implementation, we would emit an Insert event here.
break;
}
case "delete": {
// delete effector
const elt = this.getByID(msg.id);
if (elt.isDeleted) return
elt.value = null;
elt.isDeleted = true;
this._length--;
// In a production implementation, we would emit a Delete event here.
break;
}
default:
throw new Error("Bad message: " + msg);
}
// We fall through if the message hasn't been processed yet.
this.msgsInCausalOrder.push(msg)
}
private computeLeft(
id: ID,
leftOrigin: Element<T>,
rightOrigin: Element<T>
): Element<T> {
const rightParent = this.rightParent(leftOrigin, rightOrigin);
let left = leftOrigin;
let scanning = false;
// o ranges from leftOrigin to (non-adjusted) rightOrigin, *exclusive*.
// Note that o will never be start or end (within the loop),
// so its origins are non-null.
for (let o = leftOrigin.right!; o !== rightOrigin; o = o.right!) {
if (this.lessThan(o.leftOrigin!, leftOrigin)) break;
else if (o.leftOrigin === leftOrigin) {
const oRightParent = this.rightParent(o.leftOrigin, o.rightOrigin!);
if (this.lessThan(oRightParent, rightParent)) {
scanning = true;
} else if (oRightParent === rightParent) {
// o and the new elt are double siblings.
if (o.id.sender > id.sender) break;
else scanning = false;
} else {
// oRightParent > rightParent
scanning = false;
}
}
if (!scanning) left = o;
}
return left;
}
private rightParent(leftOrigin: Element<T>, rightOrigin: Element<T>): Element<T> {
if (rightOrigin === this.end || rightOrigin.leftOrigin !== leftOrigin) return this.end;
else return rightOrigin;
}
/**
* Returns whether a < b in the linked list order.
*/
private lessThan(a: Element<T>, b: Element<T>): boolean {
if (a === b) return false;
// Loop forwards from each of a & b in parallel until one finds the other.
// In principle this takes O(n) time, but in practice a & b should usually
// be close together.
let afterA = a;
let afterB = b;
while (true) {
if (afterA === b || afterB.right === null) return true;
if (afterB === a || afterA.right === null) return false;
afterA = afterA.right;
afterB = afterB.right;
}
}
private hasID(id: ID): boolean {
const bySender = this.elementsByID.get(id.sender);
if (bySender == null) return false
return bySender[id.counter] != null
}
private getByID(id: ID): Element<T> {
const bySender = this.elementsByID.get(id.sender);
if (bySender !== undefined) {
const node = bySender[id.counter];
if (node !== undefined) return node;
}
throw new Error("Unknown ID: " + JSON.stringify(id));
}
private getByIndex(index: number): Element<T> {
if (index < 0 || index >= this.length) {
throw new Error(
"Index out of range: " + index + " (length: " + this.length + ")"
);
}
// For now, do a slow linear search, but from the end b/c that's more common.
// An easy common-case optimization is to cache index "hints" like in Yjs.
// A doable aysmptotic optimization is to build a balanced tree structure
// on top of the non-deleted list elements and use that to convert between
// indices & elements in O(log(n)) time.
let remaining = this.length - 1 - index;
for (let elt = this.end.left!; elt !== this.start; elt = elt.left!) {
if (!elt.isDeleted) {
if (remaining === 0) return elt;
remaining--;
}
}
throw new Error("Index in range but not found");
}
get(index: number): T {
return this.getByIndex(index).value!;
}
*values(): IterableIterator<T> {
// Walk the linked list.
for (
let elt: Element<T> | null = this.start;
(elt = elt.right);
elt !== null
) {
if (!elt.isDeleted) yield elt.value!;
}
}
toArray(): T[] {
return [...this.values()]
}
get length(): number {
return this._length;
}
save(): Message<T>[] {
// Save the linked list (less start & end) in causal order for easy merging.
return this.msgsInCausalOrder
}
// load(bytes: Uint8Array): void {
load(save: Message<T>[]): void {
for (const msg of save) {
this.receivePrimitive(msg)
}
}
mergeFrom(other: ListFugueSimple<T>) {
const save = other.save()
this.load(save)
}
debugPrint() {
// Walk the linked list.
const depth: Record<string, number> = {}
// const kForId = (id: Id, c: T | null) => `${id[0]} ${id[1]} ${id[2] ?? c != null}`
const eltId = (elt: Element<any>) => elt.id.sender === '' ? 'ROOT' : `${elt.id.sender},${elt.id.counter}`
depth[eltId(this.start)] = 0
for (
let elt: Element<T> | null = this.start;
(elt = elt.right);
elt !== null
) {
// The only items with a null left / right are the roots.
if (elt.leftOrigin == null || elt.rightOrigin == null) continue
const isLeftChild = true
// const isLeftChild = this.rightParent(elt.leftOrigin, elt.rightOrigin) === this.end
const parent = isLeftChild ? elt.leftOrigin : elt.rightOrigin
const d = (parent === this.start || parent === this.end)
? 0
: depth[eltId(parent)] + 1
depth[eltId(elt)] = d
// let content = `${isLeftChild ? '/' : '\\'}${elt.value == null
let content = `${elt.value == null
? '.'
// : elt.isDeleted ? chalk.strikethrough(elt.value) : chalk.yellow(elt.value)
: elt.value
} at [${eltId(elt)}] (left [${eltId(elt.leftOrigin)}])`
content += ` right [${eltId(elt.rightOrigin)}]`
content += ` rightParent ${eltId(this.rightParent(elt.leftOrigin, elt.rightOrigin))}`
// console.log(`${'| '.repeat(d)}${elt.value == null ? chalk.strikethrough(content) : content}`)
console.log(`${'| '.repeat(d)}${elt.value == null ? content : content}`)
}
}
}