Skip to content

Commit ed6ce96

Browse files
committed
feat(json-crdt-extensions): 🎸 improve text hash calculation
1 parent d64b8ab commit ed6ce96

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ export class Overlay<T = string> implements Printable, Stateful {
331331
hash = this.refreshSlices(hash, txt.savedSlices);
332332
hash = this.refreshSlices(hash, txt.extraSlices);
333333
hash = this.refreshSlices(hash, txt.localSlices);
334-
if (!slicesOnly) hash = this.computeSplitTextHashes(hash);
334+
335+
// TODO: Move test hash calculation out of the overlay.
336+
if (!slicesOnly) {
337+
// hash = updateRga(hash, txt.str);
338+
hash = this.refreshTextSlices(hash);
339+
}
335340
return (this.hash = hash);
336341
}
337342

@@ -461,7 +466,7 @@ export class Overlay<T = string> implements Printable, Stateful {
461466

462467
public leadingTextHash: number = 0;
463468

464-
protected computeSplitTextHashes(stateTotal: number): number {
469+
protected refreshTextSlices(stateTotal: number): number {
465470
const txt = this.txt;
466471
const str = txt.str;
467472
const firstChunk = str.first();
@@ -472,7 +477,6 @@ export class Overlay<T = string> implements Printable, Stateful {
472477
let state: number = CONST.START_STATE;
473478
for (let pair = i(); pair; pair = i()) {
474479
const [p1, p2] = pair;
475-
// TODO: need to incorporate slice attribute hash here?
476480
const id1 = p1.id;
477481
state = (state << 5) + state + (id1.sid >>> 0) + id1.time;
478482
let overlayPointHash = CONST.START_STATE;
@@ -482,10 +486,8 @@ export class Overlay<T = string> implements Printable, Stateful {
482486
(overlayPointHash << 5) + overlayPointHash + ((((id.sid >>> 0) + id.time) << 8) + (off << 4) + len);
483487
});
484488
state = updateNum(state, overlayPointHash);
485-
if (p1) {
486-
p1.hash = overlayPointHash;
487-
stateTotal = updateNum(stateTotal, overlayPointHash);
488-
}
489+
p1.hash = overlayPointHash;
490+
stateTotal = updateNum(stateTotal, overlayPointHash);
489491
if (p2 instanceof MarkerOverlayPoint) {
490492
if (marker) {
491493
marker.textHash = state;

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {SliceBehavior} from '../../slice/constants';
66

77
const setup = () => {
88
const sid = 123456789;
9-
const model = Model.withLogicalClock(sid);
9+
const model = Model.create(undefined, sid);
1010
model.api.root({
1111
text: '',
1212
slices: [],
@@ -307,18 +307,35 @@ describe('Overlay.refresh()', () => {
307307
describe('updates hash', () => {
308308
testRefresh('when the first character is deleted and reinserted', (kit, refresh) => {
309309
const index = 0;
310-
const char = kit.peritext.strApi().view()[index];
310+
const str = kit.peritext.strApi();
311+
const char = str.view()[index];
312+
const view = str.view();
311313
refresh();
312314
kit.peritext.strApi().del(index, 1);
313315
kit.peritext.strApi().ins(index, char);
316+
expect(str.view()).toEqual(view);
314317
});
315318

316319
testRefresh('when the last character is deleted and reinserted', (kit, refresh) => {
317320
const index = kit.peritext.strApi().view().length - 1;
318-
const char = kit.peritext.strApi().view()[index];
321+
const str = kit.peritext.strApi();
322+
const char = str.view()[index];
323+
const view = str.view();
319324
refresh();
320325
kit.peritext.strApi().del(index, 1);
321326
kit.peritext.strApi().ins(index, char);
327+
expect(str.view()).toEqual(view);
328+
});
329+
330+
testRefresh('when the third character is reinserted', (kit, refresh) => {
331+
const index = 3;
332+
const str = kit.peritext.strApi();
333+
const char = str.view()[index];
334+
const view = str.view();
335+
refresh();
336+
kit.peritext.strApi().del(index, 1);
337+
kit.peritext.strApi().ins(index, char);
338+
expect(str.view()).toEqual(view);
322339
});
323340
});
324341
});

0 commit comments

Comments
 (0)