Skip to content

Commit fbb9a81

Browse files
committed
refactor(json-crdt): 💡 fix File class after refacator
1 parent 934cbe9 commit fbb9a81

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/json-crdt/file/File.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ export class File implements Printable {
4646
if (history) {
4747
const [start, patches] = history;
4848
if (start) {
49-
const startModel = decodeModel(start);
50-
log = new PatchLog(startModel);
51-
for (const patch of patches) log.push(decodePatch(patch));
49+
log = new PatchLog(() => decodeModel(start));
50+
for (const patch of patches) log.end.applyPatch(decodePatch(patch));
5251
}
5352
}
5453
if (!log) throw new Error('NO_HISTORY');
@@ -57,7 +56,7 @@ export class File implements Printable {
5756
for (const patch of frontier) {
5857
const patchDecoded = decodePatch(patch);
5958
decodedModel.applyPatch(patchDecoded);
60-
log.push(patchDecoded);
59+
log.end.applyPatch(patchDecoded);
6160
}
6261
}
6362
const file = new File(decodedModel, log);
@@ -88,7 +87,7 @@ export class File implements Printable {
8887
const id = patch.getId();
8988
if (!id) return;
9089
this.model.applyPatch(patch);
91-
this.log.push(patch);
90+
this.log.end.applyPatch(patch);
9291
}
9392

9493
/**
@@ -100,10 +99,10 @@ export class File implements Printable {
10099
const api = model.api;
101100
const autoflushUnsubscribe = api.autoFlush();
102101
const onPatchUnsubscribe = api.onPatch.listen((patch) => {
103-
log.push(patch);
102+
log.end.applyPatch(patch);
104103
});
105104
const onFlushUnsubscribe = api.onFlush.listen((patch) => {
106-
log.push(patch);
105+
log.end.applyPatch(patch);
107106
});
108107
return () => {
109108
autoflushUnsubscribe();
@@ -153,7 +152,7 @@ export class File implements Printable {
153152
const patchFormat = params.history ?? 'binary';
154153
switch (patchFormat) {
155154
case 'binary': {
156-
history[0] = this.log.start.toBinary();
155+
history[0] = this.log.start().toBinary();
157156
this.log.patches.forEach(({v}) => {
158157
history[1].push(v.toBinary());
159158
});
@@ -162,7 +161,7 @@ export class File implements Printable {
162161
case 'compact': {
163162
const encoder = this.options.structuralCompactEncoder;
164163
if (!encoder) throw new Error('NO_COMPACT_ENCODER');
165-
history[0] = encoder.encode(this.log.start);
164+
history[0] = encoder.encode(this.log.start());
166165
const encodeCompact = this.options.patchCompactEncoder;
167166
if (!encodeCompact) throw new Error('NO_COMPACT_PATCH_ENCODER');
168167
const list = history[1];
@@ -174,7 +173,7 @@ export class File implements Printable {
174173
case 'verbose': {
175174
const encoder = this.options.structuralVerboseEncoder;
176175
if (!encoder) throw new Error('NO_VERBOSE_ENCODER');
177-
history[0] = encoder.encode(this.log.start);
176+
history[0] = encoder.encode(this.log.start());
178177
const encodeVerbose = this.options.patchVerboseEncoder;
179178
if (!encodeVerbose) throw new Error('NO_VERBOSE_PATCH_ENCODER');
180179
const list = history[1];

src/json-crdt/file/__tests__/File.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ test('can create File from new model', () => {
2020
}),
2121
);
2222
const file = File.fromModel(model);
23-
expect(file.log.start.view()).toBe(undefined);
23+
expect(file.log.start().view()).toBe(undefined);
2424
expect(file.model.view()).toEqual({
2525
foo: 'bar',
2626
});
27-
expect(file.log.start.clock.sid).toBe(file.model.clock.sid);
27+
expect(file.log.start().clock.sid).toBe(file.model.clock.sid);
2828
});
2929

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

@@ -66,7 +66,7 @@ describe('.toBinary()', () => {
6666
const file2 = File.fromSeqCbor(blob);
6767
expect(file2.model.view()).toEqual({foo: 'bar'});
6868
expect(file2.model !== file.model).toBe(true);
69-
expect(file.log.start.view()).toEqual(undefined);
69+
expect(file.log.start().view()).toEqual(undefined);
7070
expect(file.log.replayToEnd().view()).toEqual({foo: 'bar'});
7171
});
7272
});
@@ -78,7 +78,7 @@ describe('.toBinary()', () => {
7878
params.format === 'seq.cbor' ? File.fromSeqCbor(blob, fileEncoders) : File.fromNdjson(blob, fileEncoders);
7979
expect(file2.model.view()).toEqual(file.model.view());
8080
expect(file2.model !== file.model).toBe(true);
81-
expect(file2.log.start.view()).toEqual(undefined);
81+
expect(file2.log.start().view()).toEqual(undefined);
8282
expect(file2.log.replayToEnd().view()).toEqual(file.model.view());
8383
expect(file2.log.patches.size()).toBe(file.log.patches.size());
8484
};

0 commit comments

Comments
 (0)