Skip to content

Commit

Permalink
refactor(json-crdt): 💡 fix File class after refacator
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 26, 2024
1 parent 934cbe9 commit fbb9a81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
19 changes: 9 additions & 10 deletions src/json-crdt/file/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ export class File implements Printable {
if (history) {
const [start, patches] = history;
if (start) {
const startModel = decodeModel(start);
log = new PatchLog(startModel);
for (const patch of patches) log.push(decodePatch(patch));
log = new PatchLog(() => decodeModel(start));
for (const patch of patches) log.end.applyPatch(decodePatch(patch));
}
}
if (!log) throw new Error('NO_HISTORY');
Expand All @@ -57,7 +56,7 @@ export class File implements Printable {
for (const patch of frontier) {
const patchDecoded = decodePatch(patch);
decodedModel.applyPatch(patchDecoded);
log.push(patchDecoded);
log.end.applyPatch(patchDecoded);
}
}
const file = new File(decodedModel, log);
Expand Down Expand Up @@ -88,7 +87,7 @@ export class File implements Printable {
const id = patch.getId();
if (!id) return;
this.model.applyPatch(patch);
this.log.push(patch);
this.log.end.applyPatch(patch);
}

/**
Expand All @@ -100,10 +99,10 @@ export class File implements Printable {
const api = model.api;
const autoflushUnsubscribe = api.autoFlush();
const onPatchUnsubscribe = api.onPatch.listen((patch) => {
log.push(patch);
log.end.applyPatch(patch);
});
const onFlushUnsubscribe = api.onFlush.listen((patch) => {
log.push(patch);
log.end.applyPatch(patch);
});
return () => {
autoflushUnsubscribe();
Expand Down Expand Up @@ -153,7 +152,7 @@ export class File implements Printable {
const patchFormat = params.history ?? 'binary';
switch (patchFormat) {
case 'binary': {
history[0] = this.log.start.toBinary();
history[0] = this.log.start().toBinary();
this.log.patches.forEach(({v}) => {
history[1].push(v.toBinary());
});
Expand All @@ -162,7 +161,7 @@ export class File implements Printable {
case 'compact': {
const encoder = this.options.structuralCompactEncoder;
if (!encoder) throw new Error('NO_COMPACT_ENCODER');
history[0] = encoder.encode(this.log.start);
history[0] = encoder.encode(this.log.start());
const encodeCompact = this.options.patchCompactEncoder;
if (!encodeCompact) throw new Error('NO_COMPACT_PATCH_ENCODER');
const list = history[1];
Expand All @@ -174,7 +173,7 @@ export class File implements Printable {
case 'verbose': {
const encoder = this.options.structuralVerboseEncoder;
if (!encoder) throw new Error('NO_VERBOSE_ENCODER');
history[0] = encoder.encode(this.log.start);
history[0] = encoder.encode(this.log.start());
const encodeVerbose = this.options.patchVerboseEncoder;
if (!encodeVerbose) throw new Error('NO_VERBOSE_PATCH_ENCODER');
const list = history[1];
Expand Down
10 changes: 5 additions & 5 deletions src/json-crdt/file/__tests__/File.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ test('can create File from new model', () => {
}),
);
const file = File.fromModel(model);
expect(file.log.start.view()).toBe(undefined);
expect(file.log.start().view()).toBe(undefined);
expect(file.model.view()).toEqual({
foo: 'bar',
});
expect(file.log.start.clock.sid).toBe(file.model.clock.sid);
expect(file.log.start().clock.sid).toBe(file.model.clock.sid);
});

test.todo('patches are flushed and stored in memory');
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('.toBinary()', () => {
const file2 = File.fromNdjson(blob);
expect(file2.model.view()).toEqual({foo: 'bar'});
expect(file2.model !== file.model).toBe(true);
expect(file.log.start.view()).toEqual(undefined);
expect(file.log.start().view()).toEqual(undefined);
expect(file.log.replayToEnd().view()).toEqual({foo: 'bar'});
});

Expand All @@ -66,7 +66,7 @@ describe('.toBinary()', () => {
const file2 = File.fromSeqCbor(blob);
expect(file2.model.view()).toEqual({foo: 'bar'});
expect(file2.model !== file.model).toBe(true);
expect(file.log.start.view()).toEqual(undefined);
expect(file.log.start().view()).toEqual(undefined);
expect(file.log.replayToEnd().view()).toEqual({foo: 'bar'});
});
});
Expand All @@ -78,7 +78,7 @@ describe('.toBinary()', () => {
params.format === 'seq.cbor' ? File.fromSeqCbor(blob, fileEncoders) : File.fromNdjson(blob, fileEncoders);
expect(file2.model.view()).toEqual(file.model.view());
expect(file2.model !== file.model).toBe(true);
expect(file2.log.start.view()).toEqual(undefined);
expect(file2.log.start().view()).toEqual(undefined);
expect(file2.log.replayToEnd().view()).toEqual(file.model.view());
expect(file2.log.patches.size()).toBe(file.log.patches.size());
};
Expand Down

0 comments on commit fbb9a81

Please sign in to comment.