Skip to content

Commit

Permalink
perf(json-crdt-extensions): ⚡️ remove immediately from the right bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 26, 2024
1 parent 8a23776 commit faf466f
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/json-crdt-extensions/peritext/overlay/OverlayPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,60 @@ export class OverlayPoint extends Point implements Printable, HeadlessNode {
*/
public readonly refs: OverlayRef[] = [];

/**
* Insert a reference to a marker.
*
* @param slice A marker (split slice).
*/
public addMarkerRef(slice: SplitSlice): void {
this.refs.push(slice);
this.addMarker(slice);
}

/**
* Insert a layer that starts at this point.
*
* @param slice A slice that starts at this point.
*/
public addLayerStartRef(slice: Slice): void {
this.refs.push(new OverlayRefSliceStart(slice));
this.addLayer(slice);
}

/**
* Insert a layer that ends at this point.
*
* @param slice A slice that ends at this point.
*/
public addLayerEndRef(slice: Slice): void {
this.refs.push(new OverlayRefSliceEnd(slice));
}

/**
* Removes a reference to a marker or a slice, and remove the corresponding
* layer or marker.
*
* @param slice A slice to remove the reference to.
*/
public removeRef(slice: Slice): void {
const refs = this.refs;
const length = refs.length;
for (let i = 0; i < length; i++) {
const ref = refs[i];
if (ref === slice) {
refs.splice(i, 1);
this.removeMarker(slice);
return;
}
if (
ref === slice ||
(ref instanceof OverlayRefSliceStart && ref.slice === slice) ||
(ref instanceof OverlayRefSliceEnd && ref.slice === slice)
) {
refs.splice(i, 1);
break;
this.removeLayer(slice);
return;
}
}
this.removeLayer(slice);
this.removeMarker(slice);
}

// ---------------------------------------------------------------- Printable
Expand Down

0 comments on commit faf466f

Please sign in to comment.