Skip to content

Commit faf466f

Browse files
committed
perf(json-crdt-extensions): ⚡️ remove immediately from the right bucket
1 parent 8a23776 commit faf466f

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/json-crdt-extensions/peritext/overlay/OverlayPoint.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,60 @@ export class OverlayPoint extends Point implements Printable, HeadlessNode {
152152
*/
153153
public readonly refs: OverlayRef[] = [];
154154

155+
/**
156+
* Insert a reference to a marker.
157+
*
158+
* @param slice A marker (split slice).
159+
*/
155160
public addMarkerRef(slice: SplitSlice): void {
156161
this.refs.push(slice);
157162
this.addMarker(slice);
158163
}
159164

165+
/**
166+
* Insert a layer that starts at this point.
167+
*
168+
* @param slice A slice that starts at this point.
169+
*/
160170
public addLayerStartRef(slice: Slice): void {
161171
this.refs.push(new OverlayRefSliceStart(slice));
162172
this.addLayer(slice);
163173
}
164174

175+
/**
176+
* Insert a layer that ends at this point.
177+
*
178+
* @param slice A slice that ends at this point.
179+
*/
165180
public addLayerEndRef(slice: Slice): void {
166181
this.refs.push(new OverlayRefSliceEnd(slice));
167182
}
168183

184+
/**
185+
* Removes a reference to a marker or a slice, and remove the corresponding
186+
* layer or marker.
187+
*
188+
* @param slice A slice to remove the reference to.
189+
*/
169190
public removeRef(slice: Slice): void {
170191
const refs = this.refs;
171192
const length = refs.length;
172193
for (let i = 0; i < length; i++) {
173194
const ref = refs[i];
195+
if (ref === slice) {
196+
refs.splice(i, 1);
197+
this.removeMarker(slice);
198+
return;
199+
}
174200
if (
175-
ref === slice ||
176201
(ref instanceof OverlayRefSliceStart && ref.slice === slice) ||
177202
(ref instanceof OverlayRefSliceEnd && ref.slice === slice)
178203
) {
179204
refs.splice(i, 1);
180-
break;
205+
this.removeLayer(slice);
206+
return;
181207
}
182208
}
183-
this.removeLayer(slice);
184-
this.removeMarker(slice);
185209
}
186210

187211
// ---------------------------------------------------------------- Printable

0 commit comments

Comments
 (0)