Skip to content

Commit 30c7302

Browse files
committed
chore: 🤖 cleanup update method
1 parent 1a862d6 commit 30c7302

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/__demos__/json-crdt-server/routes/block/methods/upd.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
import type {RouteDeps, Router, RouterBase} from '../../types';
2-
import type {BlockId, BlockPatch} from '../schema';
2+
import {BlockCurRef, BlockIdRef, BlockPatchPartialRef, BlockPatchRef} from '../schema';
33

44
export const upd =
55
({t, services}: RouteDeps) =>
66
<R extends RouterBase>(r: Router<R>) => {
7-
const PatchType = t.Ref<typeof BlockPatch>('BlockPatch');
8-
97
const Request = t.Object(
10-
t.prop('id', t.Ref<typeof BlockId>('BlockId')).options({
8+
t.prop('id', BlockIdRef).options({
119
title: 'Document ID',
1210
description: 'The ID of the document to apply the patch to.',
1311
}),
14-
// This can be inferred from the "seq" of the first patch:
15-
// t.prop('seq', t.Ref<typeof BlockSeq>('BlockSeq')).options({
16-
// title: 'Last known sequence number',
17-
// description:
18-
// 'The last known sequence number of the document. ' +
19-
// 'If the document has changed since this sequence number, ' +
20-
// 'the response will contain all the necessary patches for the client to catch up.',
21-
// }),
22-
t.prop('patches', t.Array(PatchType)).options({
12+
t.prop('cur', BlockCurRef).options({
13+
title: 'Last known sequence number',
14+
description:
15+
'The last known sequence number of the document. ' +
16+
'If the document has changed since this sequence number, ' +
17+
'the response will contain all the necessary patches for the client to catch up.',
18+
}),
19+
t.prop('patches', t.Array(BlockPatchPartialRef)).options({
2320
title: 'Patches',
2421
description: 'The patches to apply to the document.',
2522
}),
2623
);
2724

2825
const Response = t.Object(
29-
t.prop('patches', t.Array(PatchType)).options({
26+
t.prop('patches', t.Array(BlockPatchRef)).options({
3027
title: 'Latest patches',
3128
description: 'The list of patches that the client might have missed and should apply to the document.',
3229
}),
@@ -38,7 +35,7 @@ export const upd =
3835
description: 'Applies patches to an existing document and returns the latest concurrent changes.',
3936
});
4037

41-
return r.prop('block.upd', Func, async ({id, patches}) => {
38+
return r.prop('block.upd', Func, async ({id, cur, patches}) => {
4239
const res = await services.blocks.edit(id, patches);
4340
return {
4441
patches: res.patches,

src/__demos__/json-crdt-server/services/blocks/BlocksServices.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,22 @@ export class BlocksServices {
122122
return {patches, model};
123123
}
124124

125-
public async edit(id: string, patches: StorePatch[]) {
125+
public async edit(id: string, patches: Pick<StorePatch, 'blob'>[]) {
126126
this.maybeGc();
127127
if (!Array.isArray(patches)) throw RpcError.validation('patches must be an array');
128128
if (!patches.length) throw RpcError.validation('patches must not be empty');
129-
const seq = patches[0].seq;
130-
const {store} = this;
131129
validatePatches(patches);
132-
const {snapshot} = await store.edit(id, patches);
133-
this.__emitUpd(id, patches);
130+
const {store} = this;
131+
const seq = (await store.seq(id)) ?? -1;
132+
const fullPatches: StorePatch[] = [];
133+
const now = Date.now();
134+
for (let i = 0; i < patches.length; i++) {
135+
const seqNum = seq + i;
136+
const patch = patches[i];
137+
fullPatches.push({seq: seqNum, created: now, blob: patch.blob});
138+
}
139+
const {snapshot} = await store.edit(id, fullPatches);
140+
this.__emitUpd(id, fullPatches);
134141
const expectedBlockSeq = seq + patches.length - 1;
135142
const hadConcurrentEdits = snapshot.seq !== expectedBlockSeq;
136143
let patchesBack: StorePatch[] = [];

0 commit comments

Comments
 (0)