-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolf_string_fugue.ts
More file actions
38 lines (34 loc) · 1.24 KB
/
Copy pathgolf_string_fugue.ts
File metadata and controls
38 lines (34 loc) · 1.24 KB
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
import { LexUDTotalOrder } from "../lex_ud_total_order";
import { randomReplicaID } from "../utils";
/**
* A code golf version of [[StringFugue]].
*
* Its positions are the same as those of [[StringFugue]]
* except that they all start with an extra "R".
* This implementation is just for code golf; [[StringFugue]]
* has clearer code and 1-character-shorter positions.
*
* For a description of the algorithm, see
* [https://mattweidner.com/2022/10/05/basic-list-crdt.html#intro-string-implementation](https://mattweidner.com/2022/10/05/basic-list-crdt.html#intro-string-implementation)
*/
export class GolfStringFugue extends LexUDTotalOrder {
readonly id: string;
private c = 0;
/**
* @param options.replicaID A unique replicaID. Must be unique among all
* collaborating replicas, including past or concurrent replicas for the
* same device or user. All collaborating replicas' replicaIDs must be the
* same length.
*/
constructor(options: { replicaID?: string } = {}) {
super();
this.id = options.replicaID ?? randomReplicaID();
}
createBetween(a = "R", b = "Z"): string {
return (
// "One line" List CRDT :)
(b.startsWith(a) ? b.slice(0, -1) + "L" : a) + `${this.id}${this.c++}R`
);
}
// ...
}