reptree 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.cjs +149 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -12
- package/dist/index.d.ts +48 -12
- package/dist/index.js +149 -30
- package/dist/index.js.map +1 -1
- package/package.json +2 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An identifier for an operation. We use them to compare and order operations.
|
|
3
|
+
* It uses a counter as a Lamport clock and a peer ID in case if ops have the same counter - in that case the ops will be compared by peer ID.
|
|
4
|
+
* Lamport clock (Lamport timestamp): https://en.wikipedia.org/wiki/Lamport_timestamp
|
|
5
|
+
*/
|
|
1
6
|
interface OpId {
|
|
2
7
|
readonly counter: number;
|
|
3
8
|
readonly peerId: string;
|
|
@@ -10,8 +15,8 @@ declare class VertexState {
|
|
|
10
15
|
private transientProperties;
|
|
11
16
|
children: string[];
|
|
12
17
|
constructor(id: string, parentId: TreeVertexId | null);
|
|
13
|
-
setProperty(key: string, value:
|
|
14
|
-
setTransientProperty(key: string, value:
|
|
18
|
+
setProperty(key: string, value: VertexPropertyType): void;
|
|
19
|
+
setTransientProperty(key: string, value: VertexPropertyType): void;
|
|
15
20
|
getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
16
21
|
getAllProperties(includingTransient?: boolean): ReadonlyArray<TreeVertexProperty>;
|
|
17
22
|
removeProperty(key: string): void;
|
|
@@ -21,11 +26,12 @@ declare class VertexState {
|
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
type TreeVertexId = string;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
30
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
31
|
+
[key: string]: JsonValue;
|
|
32
|
+
};
|
|
33
|
+
/** Property type for state (undefined means removal) */
|
|
34
|
+
type VertexPropertyType = JsonValue | undefined;
|
|
29
35
|
type TreeVertexProperty = {
|
|
30
36
|
readonly key: string;
|
|
31
37
|
readonly value: VertexPropertyType;
|
|
@@ -162,33 +168,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
|
|
|
162
168
|
* for working with vertices in a RepTree.
|
|
163
169
|
*/
|
|
164
170
|
declare class Vertex {
|
|
165
|
-
private tree;
|
|
166
171
|
private state;
|
|
172
|
+
private _tree;
|
|
167
173
|
constructor(tree: RepTree, state: VertexState);
|
|
174
|
+
/** Returns the tree this vertex belongs to. */
|
|
175
|
+
get tree(): RepTree;
|
|
176
|
+
private set tree(value);
|
|
177
|
+
/** Returns the ID of this vertex. */
|
|
168
178
|
get id(): string;
|
|
179
|
+
/** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
169
180
|
get name(): string | undefined;
|
|
181
|
+
/** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
170
182
|
set name(name: string);
|
|
183
|
+
/** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
|
|
171
184
|
get createdAt(): Date;
|
|
172
|
-
|
|
185
|
+
/** Returns the ID of the parent vertex of this vertex. */
|
|
173
186
|
get parentId(): string | null;
|
|
187
|
+
/** Returns the parent vertex of this vertex. */
|
|
174
188
|
get parent(): Vertex | undefined;
|
|
189
|
+
/** Returns the children vertices of this vertex. */
|
|
175
190
|
get children(): Vertex[];
|
|
191
|
+
/** Returns the IDs of the children vertices of this vertex. */
|
|
176
192
|
get childrenIds(): string[];
|
|
193
|
+
/** Returns the ancestors of this vertex. The first element is the root vertex.
|
|
194
|
+
* E.g root -> grandparent -> parent.
|
|
195
|
+
* Doesn't include this vertex in the array.
|
|
196
|
+
*/
|
|
197
|
+
get ancestors(): Vertex[];
|
|
198
|
+
/** Returns the ID of the root vertex of the tree this vertex belongs to. */
|
|
199
|
+
get treeId(): string;
|
|
200
|
+
/** Returns the root vertex of the tree this vertex belongs to. */
|
|
201
|
+
get root(): Vertex;
|
|
177
202
|
getAsTypedObject<T>(): T;
|
|
178
203
|
getChildrenAsTypedArray<T>(): T[];
|
|
204
|
+
/** Creates a new child vertex of this vertex. */
|
|
179
205
|
newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
|
|
206
|
+
/** Creates a new named child vertex of this vertex. */
|
|
180
207
|
newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
|
|
208
|
+
/** Sets a property on this vertex. */
|
|
181
209
|
setProperty(key: string, value: VertexPropertyType): void;
|
|
210
|
+
/** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
|
|
182
211
|
setTransientProperty(key: string, value: VertexPropertyType): void;
|
|
212
|
+
/** Promotes all transient (temporary) properties to persistent properties. */
|
|
183
213
|
commitTransients(): void;
|
|
214
|
+
/** Sets multiple properties on this vertex. */
|
|
184
215
|
setProperties(props: Record<string, VertexPropertyType> | object): void;
|
|
216
|
+
/** Returns the value of a property on this vertex. */
|
|
185
217
|
getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
218
|
+
/** Returns all properties on this vertex. */
|
|
186
219
|
getProperties(): Record<string, VertexPropertyType>;
|
|
187
220
|
findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
|
|
188
221
|
findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
|
|
189
222
|
findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
|
|
190
223
|
findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
|
|
224
|
+
/** Observes changes to this vertex. */
|
|
191
225
|
observe(listener: (events: VertexChangeEvent[]) => void): () => void;
|
|
226
|
+
/** Observes changes to the children of this vertex. */
|
|
192
227
|
observeChildren(listener: (children: Vertex[]) => void): () => void;
|
|
193
228
|
observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
|
|
194
229
|
delete(): void;
|
|
@@ -241,6 +276,7 @@ declare class RepTree {
|
|
|
241
276
|
getParent(vertexId: string): Vertex | undefined;
|
|
242
277
|
getChildren(vertexId: string): Vertex[];
|
|
243
278
|
getChildrenIds(vertexId: string): string[];
|
|
279
|
+
/** Returns the ancestors of the given vertex. The first element is the root vertex. */
|
|
244
280
|
getAncestors(vertexId: string): Vertex[];
|
|
245
281
|
getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
246
282
|
getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
|
|
@@ -344,8 +380,8 @@ declare class TreeState {
|
|
|
344
380
|
getChildrenIds(vertexId: TreeVertexId): string[];
|
|
345
381
|
getChildren(vertexId: TreeVertexId): VertexState[];
|
|
346
382
|
moveVertex(vertexId: TreeVertexId, newParentId: TreeVertexId | null): VertexState;
|
|
347
|
-
setProperty(vertexId: string, key: string, value:
|
|
348
|
-
setTransientProperty(vertexId: string, key: string, value:
|
|
383
|
+
setProperty(vertexId: string, key: string, value: VertexPropertyType): void;
|
|
384
|
+
setTransientProperty(vertexId: string, key: string, value: VertexPropertyType): void;
|
|
349
385
|
addChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void): void;
|
|
350
386
|
removeChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void): void;
|
|
351
387
|
addGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void): void;
|
|
@@ -413,4 +449,4 @@ declare class StateVector {
|
|
|
413
449
|
|
|
414
450
|
declare function uuid(): string;
|
|
415
451
|
|
|
416
|
-
export { type BindOptions, type BindedVertex, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
|
|
452
|
+
export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An identifier for an operation. We use them to compare and order operations.
|
|
3
|
+
* It uses a counter as a Lamport clock and a peer ID in case if ops have the same counter - in that case the ops will be compared by peer ID.
|
|
4
|
+
* Lamport clock (Lamport timestamp): https://en.wikipedia.org/wiki/Lamport_timestamp
|
|
5
|
+
*/
|
|
1
6
|
interface OpId {
|
|
2
7
|
readonly counter: number;
|
|
3
8
|
readonly peerId: string;
|
|
@@ -10,8 +15,8 @@ declare class VertexState {
|
|
|
10
15
|
private transientProperties;
|
|
11
16
|
children: string[];
|
|
12
17
|
constructor(id: string, parentId: TreeVertexId | null);
|
|
13
|
-
setProperty(key: string, value:
|
|
14
|
-
setTransientProperty(key: string, value:
|
|
18
|
+
setProperty(key: string, value: VertexPropertyType): void;
|
|
19
|
+
setTransientProperty(key: string, value: VertexPropertyType): void;
|
|
15
20
|
getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
16
21
|
getAllProperties(includingTransient?: boolean): ReadonlyArray<TreeVertexProperty>;
|
|
17
22
|
removeProperty(key: string): void;
|
|
@@ -21,11 +26,12 @@ declare class VertexState {
|
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
type TreeVertexId = string;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
30
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
31
|
+
[key: string]: JsonValue;
|
|
32
|
+
};
|
|
33
|
+
/** Property type for state (undefined means removal) */
|
|
34
|
+
type VertexPropertyType = JsonValue | undefined;
|
|
29
35
|
type TreeVertexProperty = {
|
|
30
36
|
readonly key: string;
|
|
31
37
|
readonly value: VertexPropertyType;
|
|
@@ -162,33 +168,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
|
|
|
162
168
|
* for working with vertices in a RepTree.
|
|
163
169
|
*/
|
|
164
170
|
declare class Vertex {
|
|
165
|
-
private tree;
|
|
166
171
|
private state;
|
|
172
|
+
private _tree;
|
|
167
173
|
constructor(tree: RepTree, state: VertexState);
|
|
174
|
+
/** Returns the tree this vertex belongs to. */
|
|
175
|
+
get tree(): RepTree;
|
|
176
|
+
private set tree(value);
|
|
177
|
+
/** Returns the ID of this vertex. */
|
|
168
178
|
get id(): string;
|
|
179
|
+
/** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
169
180
|
get name(): string | undefined;
|
|
181
|
+
/** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
170
182
|
set name(name: string);
|
|
183
|
+
/** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
|
|
171
184
|
get createdAt(): Date;
|
|
172
|
-
|
|
185
|
+
/** Returns the ID of the parent vertex of this vertex. */
|
|
173
186
|
get parentId(): string | null;
|
|
187
|
+
/** Returns the parent vertex of this vertex. */
|
|
174
188
|
get parent(): Vertex | undefined;
|
|
189
|
+
/** Returns the children vertices of this vertex. */
|
|
175
190
|
get children(): Vertex[];
|
|
191
|
+
/** Returns the IDs of the children vertices of this vertex. */
|
|
176
192
|
get childrenIds(): string[];
|
|
193
|
+
/** Returns the ancestors of this vertex. The first element is the root vertex.
|
|
194
|
+
* E.g root -> grandparent -> parent.
|
|
195
|
+
* Doesn't include this vertex in the array.
|
|
196
|
+
*/
|
|
197
|
+
get ancestors(): Vertex[];
|
|
198
|
+
/** Returns the ID of the root vertex of the tree this vertex belongs to. */
|
|
199
|
+
get treeId(): string;
|
|
200
|
+
/** Returns the root vertex of the tree this vertex belongs to. */
|
|
201
|
+
get root(): Vertex;
|
|
177
202
|
getAsTypedObject<T>(): T;
|
|
178
203
|
getChildrenAsTypedArray<T>(): T[];
|
|
204
|
+
/** Creates a new child vertex of this vertex. */
|
|
179
205
|
newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
|
|
206
|
+
/** Creates a new named child vertex of this vertex. */
|
|
180
207
|
newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
|
|
208
|
+
/** Sets a property on this vertex. */
|
|
181
209
|
setProperty(key: string, value: VertexPropertyType): void;
|
|
210
|
+
/** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
|
|
182
211
|
setTransientProperty(key: string, value: VertexPropertyType): void;
|
|
212
|
+
/** Promotes all transient (temporary) properties to persistent properties. */
|
|
183
213
|
commitTransients(): void;
|
|
214
|
+
/** Sets multiple properties on this vertex. */
|
|
184
215
|
setProperties(props: Record<string, VertexPropertyType> | object): void;
|
|
216
|
+
/** Returns the value of a property on this vertex. */
|
|
185
217
|
getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
218
|
+
/** Returns all properties on this vertex. */
|
|
186
219
|
getProperties(): Record<string, VertexPropertyType>;
|
|
187
220
|
findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
|
|
188
221
|
findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
|
|
189
222
|
findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
|
|
190
223
|
findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
|
|
224
|
+
/** Observes changes to this vertex. */
|
|
191
225
|
observe(listener: (events: VertexChangeEvent[]) => void): () => void;
|
|
226
|
+
/** Observes changes to the children of this vertex. */
|
|
192
227
|
observeChildren(listener: (children: Vertex[]) => void): () => void;
|
|
193
228
|
observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
|
|
194
229
|
delete(): void;
|
|
@@ -241,6 +276,7 @@ declare class RepTree {
|
|
|
241
276
|
getParent(vertexId: string): Vertex | undefined;
|
|
242
277
|
getChildren(vertexId: string): Vertex[];
|
|
243
278
|
getChildrenIds(vertexId: string): string[];
|
|
279
|
+
/** Returns the ancestors of the given vertex. The first element is the root vertex. */
|
|
244
280
|
getAncestors(vertexId: string): Vertex[];
|
|
245
281
|
getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
|
|
246
282
|
getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
|
|
@@ -344,8 +380,8 @@ declare class TreeState {
|
|
|
344
380
|
getChildrenIds(vertexId: TreeVertexId): string[];
|
|
345
381
|
getChildren(vertexId: TreeVertexId): VertexState[];
|
|
346
382
|
moveVertex(vertexId: TreeVertexId, newParentId: TreeVertexId | null): VertexState;
|
|
347
|
-
setProperty(vertexId: string, key: string, value:
|
|
348
|
-
setTransientProperty(vertexId: string, key: string, value:
|
|
383
|
+
setProperty(vertexId: string, key: string, value: VertexPropertyType): void;
|
|
384
|
+
setTransientProperty(vertexId: string, key: string, value: VertexPropertyType): void;
|
|
349
385
|
addChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void): void;
|
|
350
386
|
removeChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void): void;
|
|
351
387
|
addGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void): void;
|
|
@@ -413,4 +449,4 @@ declare class StateVector {
|
|
|
413
449
|
|
|
414
450
|
declare function uuid(): string;
|
|
415
451
|
|
|
416
|
-
export { type BindOptions, type BindedVertex, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
|
|
452
|
+
export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
|
package/dist/index.js
CHANGED
|
@@ -348,7 +348,7 @@ var TreeState = class {
|
|
|
348
348
|
}
|
|
349
349
|
};
|
|
350
350
|
|
|
351
|
-
// src/uuid.ts
|
|
351
|
+
// src/utils/uuid.ts
|
|
352
352
|
var removeDashes = (guid) => guid.replace(/-/g, "");
|
|
353
353
|
function uuid() {
|
|
354
354
|
return removeDashes(crypto.randomUUID());
|
|
@@ -464,18 +464,29 @@ function bindVertex(tree, id, schemaOrOptions) {
|
|
|
464
464
|
// src/Vertex.ts
|
|
465
465
|
var Vertex = class _Vertex {
|
|
466
466
|
constructor(tree, state) {
|
|
467
|
-
this.tree = tree;
|
|
468
467
|
this.state = state;
|
|
468
|
+
this._tree = tree;
|
|
469
469
|
}
|
|
470
|
+
/** Returns the tree this vertex belongs to. */
|
|
471
|
+
get tree() {
|
|
472
|
+
return this._tree;
|
|
473
|
+
}
|
|
474
|
+
set tree(value) {
|
|
475
|
+
this._tree = value;
|
|
476
|
+
}
|
|
477
|
+
/** Returns the ID of this vertex. */
|
|
470
478
|
get id() {
|
|
471
479
|
return this.state.id;
|
|
472
480
|
}
|
|
481
|
+
/** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
473
482
|
get name() {
|
|
474
483
|
return this.getProperty("name");
|
|
475
484
|
}
|
|
485
|
+
/** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
|
|
476
486
|
set name(name) {
|
|
477
487
|
this.tree.setVertexProperty(this.id, "name", name);
|
|
478
488
|
}
|
|
489
|
+
/** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
|
|
479
490
|
get createdAt() {
|
|
480
491
|
const createdAt = this.getProperty("_c");
|
|
481
492
|
if (!createdAt) {
|
|
@@ -483,30 +494,51 @@ var Vertex = class _Vertex {
|
|
|
483
494
|
}
|
|
484
495
|
return new Date(createdAt);
|
|
485
496
|
}
|
|
486
|
-
|
|
487
|
-
throw new Error("Not implemented");
|
|
488
|
-
}
|
|
497
|
+
/** Returns the ID of the parent vertex of this vertex. */
|
|
489
498
|
get parentId() {
|
|
490
499
|
return this.state.parentId;
|
|
491
500
|
}
|
|
501
|
+
/** Returns the parent vertex of this vertex. */
|
|
492
502
|
get parent() {
|
|
493
503
|
if (!this.parentId) {
|
|
494
504
|
return void 0;
|
|
495
505
|
}
|
|
496
506
|
return this.tree.getVertex(this.parentId);
|
|
497
507
|
}
|
|
508
|
+
/** Returns the children vertices of this vertex. */
|
|
498
509
|
get children() {
|
|
499
510
|
return this.tree.getChildren(this.id);
|
|
500
511
|
}
|
|
512
|
+
/** Returns the IDs of the children vertices of this vertex. */
|
|
501
513
|
get childrenIds() {
|
|
502
514
|
return this.tree.getChildrenIds(this.id);
|
|
503
515
|
}
|
|
516
|
+
/** Returns the ancestors of this vertex. The first element is the root vertex.
|
|
517
|
+
* E.g root -> grandparent -> parent.
|
|
518
|
+
* Doesn't include this vertex in the array.
|
|
519
|
+
*/
|
|
520
|
+
get ancestors() {
|
|
521
|
+
return this.tree.getAncestors(this.id);
|
|
522
|
+
}
|
|
523
|
+
/** Returns the ID of the root vertex of the tree this vertex belongs to. */
|
|
524
|
+
get treeId() {
|
|
525
|
+
return this.root.id;
|
|
526
|
+
}
|
|
527
|
+
/** Returns the root vertex of the tree this vertex belongs to. */
|
|
528
|
+
get root() {
|
|
529
|
+
const root = this.tree.root;
|
|
530
|
+
if (!root) {
|
|
531
|
+
throw new Error("Root vertex of the tree is not set");
|
|
532
|
+
}
|
|
533
|
+
return root;
|
|
534
|
+
}
|
|
504
535
|
getAsTypedObject() {
|
|
505
536
|
return this.getProperties();
|
|
506
537
|
}
|
|
507
538
|
getChildrenAsTypedArray() {
|
|
508
539
|
return this.children.map((v) => v.getAsTypedObject());
|
|
509
540
|
}
|
|
541
|
+
/** Creates a new child vertex of this vertex. */
|
|
510
542
|
newChild(props) {
|
|
511
543
|
if (props && typeof props === "object" && "children" in props) {
|
|
512
544
|
throw new Error("Passing children inside props is not supported at the moment");
|
|
@@ -514,13 +546,15 @@ var Vertex = class _Vertex {
|
|
|
514
546
|
const normalized = _Vertex.normalizePropsForCreation(props);
|
|
515
547
|
return this.tree.newVertex(this.id, normalized);
|
|
516
548
|
}
|
|
549
|
+
/** Creates a new named child vertex of this vertex. */
|
|
517
550
|
newNamedChild(name, props) {
|
|
518
551
|
if (props && typeof props === "object" && "children" in props) {
|
|
519
552
|
throw new Error("Passing children inside props is not supported at the moment");
|
|
520
553
|
}
|
|
521
|
-
const normalized = _Vertex.normalizePropsForCreation(props
|
|
554
|
+
const normalized = _Vertex.normalizePropsForCreation(props);
|
|
522
555
|
return this.tree.newNamedVertex(this.id, name, normalized);
|
|
523
556
|
}
|
|
557
|
+
/** Sets a property on this vertex. */
|
|
524
558
|
setProperty(key, value) {
|
|
525
559
|
const existingValue = this.getProperty(key, false);
|
|
526
560
|
if (existingValue === value) {
|
|
@@ -528,6 +562,7 @@ var Vertex = class _Vertex {
|
|
|
528
562
|
}
|
|
529
563
|
this.tree.setVertexProperty(this.id, key, value);
|
|
530
564
|
}
|
|
565
|
+
/** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
|
|
531
566
|
setTransientProperty(key, value) {
|
|
532
567
|
const existingValue = this.getProperty(key);
|
|
533
568
|
if (existingValue === value) {
|
|
@@ -535,17 +570,21 @@ var Vertex = class _Vertex {
|
|
|
535
570
|
}
|
|
536
571
|
this.tree.setTransientVertexProperty(this.id, key, value);
|
|
537
572
|
}
|
|
573
|
+
/** Promotes all transient (temporary) properties to persistent properties. */
|
|
538
574
|
commitTransients() {
|
|
539
575
|
this.tree.commitTransients(this.id);
|
|
540
576
|
}
|
|
577
|
+
/** Sets multiple properties on this vertex. */
|
|
541
578
|
setProperties(props) {
|
|
542
579
|
for (const [key, value] of Object.entries(props)) {
|
|
543
580
|
this.setProperty(key, value);
|
|
544
581
|
}
|
|
545
582
|
}
|
|
583
|
+
/** Returns the value of a property on this vertex. */
|
|
546
584
|
getProperty(key, includingTransient = true) {
|
|
547
585
|
return this.tree.getVertexProperty(this.id, key, includingTransient);
|
|
548
586
|
}
|
|
587
|
+
/** Returns all properties on this vertex. */
|
|
549
588
|
getProperties() {
|
|
550
589
|
const props = {};
|
|
551
590
|
this.tree.getVertexProperties(this.id).forEach((p) => {
|
|
@@ -565,10 +604,12 @@ var Vertex = class _Vertex {
|
|
|
565
604
|
findAllTypedChildrenWithProperty(key, value) {
|
|
566
605
|
return this.findAllChildrenWithProperty(key, value).map((c) => c.getAsTypedObject());
|
|
567
606
|
}
|
|
607
|
+
/** Observes changes to this vertex. */
|
|
568
608
|
observe(listener) {
|
|
569
609
|
const unobserve = this.tree.observe(this.id, listener);
|
|
570
610
|
return () => unobserve();
|
|
571
611
|
}
|
|
612
|
+
/** Observes changes to the children of this vertex. */
|
|
572
613
|
observeChildren(listener) {
|
|
573
614
|
const unobserve = this.tree.observe(this.id, (events) => {
|
|
574
615
|
if (events.some((e) => e.type === "children")) {
|
|
@@ -597,21 +638,32 @@ var Vertex = class _Vertex {
|
|
|
597
638
|
* - Filters unsupported field types with a console warning
|
|
598
639
|
* - When a name param is provided to newNamedChild, ignores conflicting name in props
|
|
599
640
|
*/
|
|
600
|
-
static normalizePropsForCreation(props
|
|
641
|
+
static normalizePropsForCreation(props) {
|
|
601
642
|
if (!props) return null;
|
|
602
643
|
const input = props;
|
|
603
644
|
const out = {};
|
|
604
645
|
const skipped = [];
|
|
646
|
+
const isJsonValue2 = (v) => {
|
|
647
|
+
if (v === null) return true;
|
|
648
|
+
const t = typeof v;
|
|
649
|
+
if (t === "string" || t === "number" || t === "boolean") return true;
|
|
650
|
+
if (Array.isArray(v)) return v.every(isJsonValue2);
|
|
651
|
+
if (t === "object") {
|
|
652
|
+
const proto = Object.getPrototypeOf(v);
|
|
653
|
+
if (proto !== Object.prototype && proto !== null) return false;
|
|
654
|
+
for (const val of Object.values(v)) {
|
|
655
|
+
if (!isJsonValue2(val)) return false;
|
|
656
|
+
}
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
return false;
|
|
660
|
+
};
|
|
605
661
|
for (const [rawKey, rawValue] of Object.entries(input)) {
|
|
606
662
|
if (rawValue === void 0) {
|
|
607
663
|
continue;
|
|
608
664
|
}
|
|
609
665
|
if (rawKey === "children") continue;
|
|
610
666
|
let key = rawKey;
|
|
611
|
-
if (rawKey === "name" && explicitName !== void 0) {
|
|
612
|
-
console.warn('newNamedChild: "name" in props is ignored because a name argument was provided');
|
|
613
|
-
continue;
|
|
614
|
-
}
|
|
615
667
|
let value = rawValue;
|
|
616
668
|
if (key === "_c") {
|
|
617
669
|
if (value instanceof Date) {
|
|
@@ -622,23 +674,14 @@ var Vertex = class _Vertex {
|
|
|
622
674
|
continue;
|
|
623
675
|
}
|
|
624
676
|
}
|
|
625
|
-
|
|
626
|
-
if (Array.isArray(value)) {
|
|
627
|
-
if (!value.every(isPrimitive)) {
|
|
628
|
-
skipped.push(rawKey);
|
|
629
|
-
continue;
|
|
630
|
-
}
|
|
631
|
-
} else if (typeof value === "object" && value !== null) {
|
|
632
|
-
skipped.push(rawKey);
|
|
633
|
-
continue;
|
|
634
|
-
} else if (!isPrimitive(value)) {
|
|
677
|
+
if (!isJsonValue2(value)) {
|
|
635
678
|
skipped.push(rawKey);
|
|
636
679
|
continue;
|
|
637
680
|
}
|
|
638
681
|
out[key] = value;
|
|
639
682
|
}
|
|
640
683
|
if (skipped.length > 0) {
|
|
641
|
-
|
|
684
|
+
throw new Error(`Unsupported property types for keys: ${skipped.join(", ")}`);
|
|
642
685
|
}
|
|
643
686
|
return Object.keys(out).length > 0 ? out : null;
|
|
644
687
|
}
|
|
@@ -829,6 +872,61 @@ var StateVector = class _StateVector {
|
|
|
829
872
|
}
|
|
830
873
|
};
|
|
831
874
|
|
|
875
|
+
// src/utils/deepEqual.ts
|
|
876
|
+
function deepEqual(a, b) {
|
|
877
|
+
if (a === b) return true;
|
|
878
|
+
if (a === null || b === null) return a === b;
|
|
879
|
+
const ta = typeof a;
|
|
880
|
+
const tb = typeof b;
|
|
881
|
+
if (ta !== tb) return false;
|
|
882
|
+
if (ta !== "object") return false;
|
|
883
|
+
const aIsArr = Array.isArray(a);
|
|
884
|
+
const bIsArr = Array.isArray(b);
|
|
885
|
+
if (aIsArr || bIsArr) {
|
|
886
|
+
if (!(aIsArr && bIsArr)) return false;
|
|
887
|
+
if (a.length !== b.length) return false;
|
|
888
|
+
for (let i = 0; i < a.length; i++) {
|
|
889
|
+
if (!deepEqual(a[i], b[i])) return false;
|
|
890
|
+
}
|
|
891
|
+
return true;
|
|
892
|
+
}
|
|
893
|
+
const aProto = Object.getPrototypeOf(a);
|
|
894
|
+
const bProto = Object.getPrototypeOf(b);
|
|
895
|
+
if (aProto !== Object.prototype && aProto !== null || bProto !== Object.prototype && bProto !== null) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
const aKeys = Object.keys(a);
|
|
899
|
+
const bKeys = Object.keys(b);
|
|
900
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
901
|
+
for (const key of aKeys) {
|
|
902
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
|
|
903
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
904
|
+
}
|
|
905
|
+
return true;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// src/utils/isJsonValue.ts
|
|
909
|
+
function isJsonValue(v) {
|
|
910
|
+
if (v === void 0) return true;
|
|
911
|
+
if (v === null) return true;
|
|
912
|
+
const t = typeof v;
|
|
913
|
+
if (t === "string" || t === "number" || t === "boolean") return true;
|
|
914
|
+
if (t === "bigint" || t === "function" || t === "symbol") return false;
|
|
915
|
+
if (Array.isArray(v)) return v.every(isJsonValue);
|
|
916
|
+
if (t === "object") {
|
|
917
|
+
if (v instanceof Date) return false;
|
|
918
|
+
if (v instanceof Map || v instanceof Set || v instanceof RegExp) return false;
|
|
919
|
+
if (ArrayBuffer.isView(v)) return false;
|
|
920
|
+
const proto = Object.getPrototypeOf(v);
|
|
921
|
+
if (proto !== Object.prototype && proto !== null) return false;
|
|
922
|
+
for (const val of Object.values(v)) {
|
|
923
|
+
if (!isJsonValue(val)) return false;
|
|
924
|
+
}
|
|
925
|
+
return true;
|
|
926
|
+
}
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
|
|
832
930
|
// src/RepTree.ts
|
|
833
931
|
var _RepTree = class _RepTree {
|
|
834
932
|
/**
|
|
@@ -841,7 +939,6 @@ var _RepTree = class _RepTree {
|
|
|
841
939
|
this.setPropertyOps = [];
|
|
842
940
|
this.propertiesAndTheirOpIds = /* @__PURE__ */ new Map();
|
|
843
941
|
this.transientPropertiesAndTheirOpIds = /* @__PURE__ */ new Map();
|
|
844
|
-
// Observers for non-structural properties are not used
|
|
845
942
|
this.localOps = [];
|
|
846
943
|
this.pendingMovesWithMissingParent = /* @__PURE__ */ new Map();
|
|
847
944
|
this.pendingPropertiesWithMissingVertex = /* @__PURE__ */ new Map();
|
|
@@ -906,6 +1003,7 @@ var _RepTree = class _RepTree {
|
|
|
906
1003
|
getChildrenIds(vertexId) {
|
|
907
1004
|
return this.state.getChildrenIds(vertexId);
|
|
908
1005
|
}
|
|
1006
|
+
/** Returns the ancestors of the given vertex. The first element is the root vertex. */
|
|
909
1007
|
getAncestors(vertexId) {
|
|
910
1008
|
const ancestors = [];
|
|
911
1009
|
let currentVertex = this.state.getVertex(vertexId);
|
|
@@ -995,6 +1093,9 @@ var _RepTree = class _RepTree {
|
|
|
995
1093
|
this.moveVertex(vertexId, _RepTree.NULL_VERTEX_ID);
|
|
996
1094
|
}
|
|
997
1095
|
setTransientVertexProperty(vertexId, key, value) {
|
|
1096
|
+
if (!isJsonValue(value)) {
|
|
1097
|
+
throw new Error(`Unsupported transient property value for key "${key}"`);
|
|
1098
|
+
}
|
|
998
1099
|
this.lamportClock++;
|
|
999
1100
|
const op = newSetTransientVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value);
|
|
1000
1101
|
this.localOps.push(op);
|
|
@@ -1020,6 +1121,29 @@ var _RepTree = class _RepTree {
|
|
|
1020
1121
|
vertex.clearAllTransientProperties();
|
|
1021
1122
|
}
|
|
1022
1123
|
setVertexProperty(vertexId, key, value) {
|
|
1124
|
+
const isJsonValue2 = (v) => {
|
|
1125
|
+
if (v === void 0) return true;
|
|
1126
|
+
if (v === null) return true;
|
|
1127
|
+
const t = typeof v;
|
|
1128
|
+
if (t === "string" || t === "number" || t === "boolean") return true;
|
|
1129
|
+
if (t === "bigint" || t === "function" || t === "symbol") return false;
|
|
1130
|
+
if (Array.isArray(v)) return v.every(isJsonValue2);
|
|
1131
|
+
if (t === "object") {
|
|
1132
|
+
if (v instanceof Date) return false;
|
|
1133
|
+
if (v instanceof Map || v instanceof Set || v instanceof RegExp) return false;
|
|
1134
|
+
if (ArrayBuffer.isView(v)) return false;
|
|
1135
|
+
const proto = Object.getPrototypeOf(v);
|
|
1136
|
+
if (proto !== Object.prototype && proto !== null) return false;
|
|
1137
|
+
for (const val of Object.values(v)) {
|
|
1138
|
+
if (!isJsonValue2(val)) return false;
|
|
1139
|
+
}
|
|
1140
|
+
return true;
|
|
1141
|
+
}
|
|
1142
|
+
return false;
|
|
1143
|
+
};
|
|
1144
|
+
if (!isJsonValue2(value)) {
|
|
1145
|
+
throw new Error(`Unsupported property value for key "${key}"`);
|
|
1146
|
+
}
|
|
1023
1147
|
this.lamportClock++;
|
|
1024
1148
|
const op = newSetVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value);
|
|
1025
1149
|
this.localOps.push(op);
|
|
@@ -1181,12 +1305,8 @@ var _RepTree = class _RepTree {
|
|
|
1181
1305
|
}
|
|
1182
1306
|
for (const propA of propertiesA) {
|
|
1183
1307
|
const propB = propertiesB.find((p) => p.key === propA.key);
|
|
1184
|
-
if (!propB)
|
|
1185
|
-
|
|
1186
|
-
}
|
|
1187
|
-
if (propA.value !== propB.value) {
|
|
1188
|
-
return false;
|
|
1189
|
-
}
|
|
1308
|
+
if (!propB) return false;
|
|
1309
|
+
if (!deepEqual(propA.value, propB.value)) return false;
|
|
1190
1310
|
}
|
|
1191
1311
|
}
|
|
1192
1312
|
for (const childId of childrenA) {
|
|
@@ -1316,7 +1436,6 @@ var _RepTree = class _RepTree {
|
|
|
1316
1436
|
}
|
|
1317
1437
|
}
|
|
1318
1438
|
}
|
|
1319
|
-
// Non-LWW modify-property flow removed
|
|
1320
1439
|
applyOperation(op) {
|
|
1321
1440
|
if (isMoveVertexOp(op)) {
|
|
1322
1441
|
this.applyMove(op);
|