-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathsegmentGroupCollection.ts
More file actions
70 lines (57 loc) · 2.17 KB
/
Copy pathsegmentGroupCollection.ts
File metadata and controls
70 lines (57 loc) · 2.17 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
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { DoublyLinkedList, walkList } from "@fluidframework/core-utils/internal";
import type { SegmentGroup, ISegmentLeaf } from "./mergeTreeNodes.js";
import type { PropertySet } from "./properties.js";
export class SegmentGroupCollection {
private readonly segmentGroups: DoublyLinkedList<SegmentGroup>;
constructor(private readonly segment: ISegmentLeaf) {
this.segmentGroups = new DoublyLinkedList<SegmentGroup>();
}
public get size(): number {
return this.segmentGroups.length;
}
public get empty(): boolean {
return this.segmentGroups.empty;
}
public enqueue(segmentGroup: SegmentGroup): void {
this.segmentGroups.push(segmentGroup);
segmentGroup.segments.push(this.segment);
}
public dequeue(): SegmentGroup | undefined {
return this.segmentGroups.shift()?.data;
}
public remove(segmentGroup: SegmentGroup): boolean {
const found = this.segmentGroups.find((v) => v.data === segmentGroup);
if (found === undefined) {
return false;
}
this.segmentGroups.remove(found);
return true;
}
public pop(): SegmentGroup | undefined {
return this.segmentGroups.pop ? this.segmentGroups.pop()?.data : undefined;
}
public copyTo(segmentGroups: SegmentGroupCollection): void {
walkList(this.segmentGroups, (sg) => segmentGroups.enqueueOnCopy(sg.data, this.segment));
}
/**
* Returns the previousProps entry paired with this collection's segment within the given
* segmentGroup, or undefined if the group has no previousProps or no entry exists for the segment.
*/
public previousPropsForSegment(segmentGroup: SegmentGroup): PropertySet | undefined {
return segmentGroup.previousProps?.get(this.segment);
}
private enqueueOnCopy(segmentGroup: SegmentGroup, sourceSegment: ISegmentLeaf): void {
this.enqueue(segmentGroup);
if (segmentGroup.previousProps) {
// duplicate the previousProps entry for the destination segment, keyed off the source's entry
const sourceProps = segmentGroup.previousProps.get(sourceSegment);
if (sourceProps !== undefined) {
segmentGroup.previousProps.set(this.segment, sourceProps);
}
}
}
}