Skip to content

Commit 52cf2b9

Browse files
committed
test(json-crdt-extensions): 💍 pass Overlay smoke tests after refactor
1 parent 553c9ee commit 52cf2b9

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

src/json-crdt-extensions/peritext/Peritext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class Peritext implements Printable {
5959
this.extraSlices = new Slices(extraModel, extraModel.root.node().get(0)!, this.str);
6060

6161
// TODO: flush patches
62+
// TODO: remove `arr` tombstones
6263
const localModel = Model
6364
.withLogicalClock(SESSION.LOCAL)
6465
.setSchema(s.vec(s.arr([])));

src/json-crdt-extensions/peritext/editor/Cursor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Point} from '../rga/Point';
2+
import {Range} from '../rga/Range';
23
import {CursorAnchor} from '../slice/constants';
34
import {PersistedSlice} from '../slice/PersistedSlice';
45

@@ -28,15 +29,20 @@ export class Cursor<T = string> extends PersistedSlice<T> {
2829
});
2930
}
3031

32+
/** Move to persisted slice. */
3133
public setAt(start: number, length: number = 0): void {
3234
let at = start;
3335
let len = length;
3436
if (len < 0) {
3537
at += len;
3638
len = -len;
3739
}
38-
super.setAt(at, len);
39-
this.anchorSide = length < 0 ? CursorAnchor.End : CursorAnchor.Start;
40+
const range = Range.at<T>(this.rga, start, length);
41+
const anchorSide = this.anchorSide;
42+
this.update({
43+
range,
44+
type: anchorSide !== this.anchorSide ? anchorSide : undefined,
45+
});
4046
}
4147

4248
/**

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Peritext} from '../Peritext';
1515
import type {Stateful} from '../types';
1616
import type {Printable} from 'tree-dump/lib/types';
1717
import type {MutableSlice, Slice} from '../slice/types';
18+
import type {Slices} from '../slice/Slices';
1819

1920
export class Overlay implements Printable, Stateful {
2021
public root: OverlayPoint | undefined = undefined;
@@ -91,8 +92,10 @@ export class Overlay implements Printable, Stateful {
9192
public hash: number = 0;
9293

9394
public refresh(slicesOnly: boolean = false): number {
95+
const txt = this.txt;
9496
let hash: number = CONST.START_STATE;
95-
hash = this.refreshSlices(hash);
97+
hash = this.refreshSlices(hash, txt.savedSlices);
98+
hash = this.refreshSlices(hash, txt.localSlices);
9699
// hash = this.refreshCursor(hash);
97100
// TODO: refresh ephemeral slices
98101
// if (!slicesOnly) this.computeSplitTextHashes();
@@ -101,15 +104,13 @@ export class Overlay implements Printable, Stateful {
101104

102105
public readonly slices = new Map<Slice, [start: OverlayPoint, end: OverlayPoint]>();
103106

104-
private refreshSlices(state: number): number {
105-
const slices = this.txt.savedSlices;
107+
private refreshSlices(state: number, slices: Slices): number {
106108
const oldSlicesHash = slices.hash;
107109
const changed = oldSlicesHash !== slices.refresh();
108110
const sliceSet = this.slices;
109111
state = updateNum(state, slices.hash);
110112
if (changed) {
111113
slices.forEach((slice) => {
112-
// console.log('slice', slice + '');
113114
let tuple: [start: OverlayPoint, end: OverlayPoint] | undefined = sliceSet.get(slice);
114115
if (tuple) {
115116
if ((slice as any).isDel && (slice as any).isDel()) {

src/json-crdt-extensions/peritext/overlay/__tests__/Overlay.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('markers', () => {
4646
const points = [];
4747
let point;
4848
for (const iterator = peritext.overlay.iterator(); (point = iterator()); ) points.push(point);
49-
// console.log(peritext + '');
5049
expect(points.length).toBe(2);
5150
point = points[0];
5251
expect(point.pos()).toBe(5);

src/json-crdt-extensions/peritext/slice/PersistedSlice.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {hasOwnProperty} from '@jsonjoy.com/util/lib/hasOwnProperty';
12
import {Point} from '../rga/Point';
23
import {Range} from '../rga/Range';
34
import {updateNode} from '../../../json-crdt/hash';
@@ -73,31 +74,30 @@ export class PersistedSlice<T = string> extends Range<T> implements MutableSlice
7374

7475
public update(params: SliceUpdateParams<T>): void {
7576
let updateHeader = false;
76-
const {start, end} = this;
7777
const changes: [number, unknown][] = [];
7878
if (params.behavior !== undefined) {
7979
this.behavior = params.behavior;
8080
updateHeader = true;
8181
}
8282
if (params.range) {
8383
const range = params.range;
84-
if (range.start.anchor !== start.anchor) updateHeader = true;
85-
if (range.end.anchor !== end.anchor) updateHeader = true;
86-
if (compare(range.start.id, start.id) !== 0) changes.push([SliceTupleIndex.X1, s.con(range.start.id)]);
87-
if (compare(range.end.id, end.id) !== 0) changes.push([SliceTupleIndex.X2, s.con(range.end.id)]);
84+
updateHeader = true;
85+
changes.push(
86+
[SliceTupleIndex.X1, s.con(range.start.id)],
87+
[SliceTupleIndex.X2, s.con(range.end.id)]);
8888
this.start = range.start;
8989
this.end = range.start === range.end ? range.end.clone() : range.end;
9090
}
9191
if (params.type !== undefined) {
9292
this.type = params.type;
9393
changes.push([SliceTupleIndex.Type, s.con(this.type)]);
9494
}
95-
if (params.data !== undefined) changes.push([SliceTupleIndex.Data, s.con(params.data)]);
95+
if (hasOwnProperty(params, 'data')) changes.push([SliceTupleIndex.Data, s.con(params.data)]);
9696
if (updateHeader) {
9797
const header =
98-
(this.behavior << SliceHeaderShift.Behavior) +
99-
(this.start.anchor << SliceHeaderShift.X1Anchor) +
100-
(this.end.anchor << SliceHeaderShift.X2Anchor);
98+
(this.behavior << SliceHeaderShift.Behavior) +
99+
(this.start.anchor << SliceHeaderShift.X1Anchor) +
100+
(this.end.anchor << SliceHeaderShift.X2Anchor);
101101
changes.push([SliceTupleIndex.Header, s.con(header)]);
102102
}
103103
this.tupleApi().set(changes);

0 commit comments

Comments
 (0)