Skip to content

Commit 008de8d

Browse files
committed
feat(reactive-rpc): 🎸 return whole history on block read
1 parent a760cbd commit 008de8d

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

src/server/__tests__/blocks.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,67 @@ describe('blocks.*', () => {
422422
});
423423
});
424424
});
425+
426+
describe('blocks.get', () => {
427+
test('returns whole history when block is loaded', async () => {
428+
const {client} = setup();
429+
const model = Model.withLogicalClock();
430+
model.api.root({
431+
text: 'Hell',
432+
});
433+
const patch1 = model.api.flush();
434+
await client.call('blocks.create', {
435+
id: 'my-block',
436+
patches: [
437+
{
438+
seq: 0,
439+
created: Date.now(),
440+
blob: patch1.toBinary(),
441+
},
442+
],
443+
});
444+
model.api.str(['text']).ins(4, 'o');
445+
const patch2 = model.api.flush();
446+
model.api.obj([]).set({
447+
age: 26,
448+
});
449+
const patch3 = model.api.flush();
450+
await client.call('blocks.edit', {
451+
id: 'my-block',
452+
patches: [
453+
{
454+
seq: 1,
455+
created: Date.now(),
456+
blob: patch2.toBinary(),
457+
},
458+
{
459+
seq: 2,
460+
created: Date.now(),
461+
blob: patch3.toBinary(),
462+
},
463+
],
464+
});
465+
const result = await client.call('blocks.get', {id: 'my-block'});
466+
expect(result).toMatchObject({
467+
block: expect.any(Object),
468+
patches: [
469+
{
470+
seq: 0,
471+
created: expect.any(Number),
472+
blob: patch1.toBinary(),
473+
},
474+
{
475+
seq: 1,
476+
created: expect.any(Number),
477+
blob: patch2.toBinary(),
478+
},
479+
{
480+
seq: 2,
481+
created: expect.any(Number),
482+
blob: patch3.toBinary(),
483+
},
484+
],
485+
});
486+
});
487+
});
425488
});

src/server/routes/blocks/methods/get.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {RoutesBase, TypeRouter} from '../../../../json-type/system/TypeRouter';
22
import type {RouteDeps} from '../../types';
3-
import type {Block, BlockId} from '../schema';
3+
import type {Block, BlockId, BlockPatch} from '../schema';
44

55
export const get =
66
({services}: RouteDeps) =>
@@ -14,7 +14,13 @@ export const get =
1414
}),
1515
);
1616

17-
const Response = t.Object(t.prop('block', t.Ref<typeof Block>('Block').options({})));
17+
const Response = t.Object(
18+
t.prop('block', t.Ref<typeof Block>('Block').options({})),
19+
t.prop('patches', t.Array(t.Ref<typeof BlockPatch>('BlockPatch'))).options({
20+
title: 'Patches',
21+
description: 'The list of all patches.',
22+
}),
23+
);
1824

1925
const Func = t
2026
.Function(Request, Response)
@@ -24,9 +30,10 @@ export const get =
2430
description: 'Fetches a block by ID.',
2531
})
2632
.implement(async ({id}) => {
27-
const {block} = await services.blocks.get(id);
33+
const {block, patches} = await services.blocks.get(id);
2834
return {
29-
block: block,
35+
block,
36+
patches,
3037
};
3138
});
3239

src/server/services/blocks/BlocksServices.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ export class BlocksServices {
3737
const {store} = this;
3838
const result = await store.get(id);
3939
if (!result) throw RpcError.fromCode(RpcErrorCodes.NOT_FOUND);
40+
const patches = await store.history(id, 0, result.block.seq);
4041
const {block} = result;
41-
return {block};
42+
return {block, patches};
4243
}
4344

4445
public async remove(id: string) {

0 commit comments

Comments
 (0)