Skip to content

Commit 49de3cc

Browse files
committed
feat: 🎸 update the remote.update() method
1 parent 7c5da54 commit 49de3cc

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ResolveType} from 'json-joy/lib/json-type';
22
import type {RouteDeps, Router, RouterBase} from '../../types';
3-
import {BlockCurRef, BlockIdRef, BlockPatchPartialRef, BlockPatchPartialReturnRef} from '../schema';
3+
import {BlockIdRef, BlockPatchPartialRef, BlockPatchPartialReturnRef} from '../schema';
44

55
export const upd =
66
({t, services}: RouteDeps) =>

src/json-crdt-repo/remote/DemoServerRemoteHistory.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,17 @@ export class DemoServerRemoteHistory
7979

8080
public async update(
8181
id: string,
82-
cursor: Cursor,
8382
patches: Pick<DemoServerPatch, 'blob'>[],
8483
): Promise<{patches: Omit<DemoServerPatch, 'blob'>[]}> {
85-
throw new Error('Method not implemented.');
86-
// const res = await this.client.call('block.upd', {
87-
// id,
88-
// patches: patches.map((patch, seq) => ({
89-
// seq,
90-
// created: Date.now(),
91-
// blob: patch.blob,
92-
// })),
93-
// });
94-
// return {
95-
// cursor: res.patches.length ? res.patches[res.patches.length - 1].seq : cursor,
96-
// patches: res.patches,
97-
// };
84+
const res = await this.client.call('block.upd', {
85+
id,
86+
patches: patches.map((patch) => ({
87+
blob: patch.blob,
88+
})),
89+
});
90+
return {
91+
patches: res.patches,
92+
};
9893
}
9994

10095
public async delete(id: string): Promise<void> {

src/json-crdt-repo/remote/__tests__/DemoServerRemoteHistory.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ describe('.read()', () => {
9393
});
9494
});
9595

96+
describe('.update()', () => {
97+
test('can apply changes to an empty document', async () => {
98+
const {remote, caller} = await setup();
99+
const id = genId();
100+
await remote.create(id, []);
101+
const read1 = await remote.read(id);
102+
const model1 = Model.fromBinary(read1.block.snapshot.blob);
103+
expect(model1.view()).toBe(undefined);
104+
const model = Model.withLogicalClock();
105+
model.api.root({score: 42});
106+
const patch = model.api.flush();
107+
const blob = patch.toBinary();
108+
const update = await remote.update(id, [{blob}]);
109+
expect(update).toMatchObject({
110+
patches: [{
111+
ts: expect.any(Number),
112+
}]
113+
});
114+
const read2 = await remote.read(id);
115+
const model2 = Model.fromBinary(read2.block.snapshot.blob);
116+
expect(model2.view()).toEqual({score: 42});
117+
});
118+
});
119+
96120
describe('.delete()', () => {
97121
test('can delete an existing block', async () => {
98122
const {remote, caller} = await setup();

src/json-crdt-repo/remote/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface RemoteHistory<
8383
* @param cursor The cursor of the last known model state of the block.
8484
* @param patches A list of patches to apply to the block.
8585
*/
86-
update(id: string, cursor: Cursor, patches: Pick<P, 'blob'>[]): Promise<{patches: Omit<P, 'blob'>[]}>;
86+
update(id: string, patches: Pick<P, 'blob'>[]): Promise<{patches: Omit<P, 'blob'>[]}>;
8787

8888
/**
8989
* Delete the block. If not implemented, means that the protocol does not

0 commit comments

Comments
 (0)