Skip to content

Commit d805b21

Browse files
committed
feat: 🎸 cleanup block.scan method
1 parent 1894886 commit d805b21

File tree

4 files changed

+68
-89
lines changed

4 files changed

+68
-89
lines changed

src/__demos__/json-crdt-server/routes/block/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ export const block =
4141
( upd(d)
4242
( del(d)
4343
// ( listen(d)
44-
// ( scan(d)
45-
( r ))))));
44+
( scan(d)
45+
( r )))))));
4646
};
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {BlockPatch, BlockId} from '../schema';
1+
import {BlockIdRef, BlockCurRef, BlockPatchRef} from '../schema';
22
import type {RouteDeps, Router, RouterBase} from '../../types';
33

44
export const scan =
55
({t, services}: RouteDeps) =>
66
<R extends RouterBase>(r: Router<R>) => {
77
const Request = t.Object(
8-
t.prop('id', t.Ref<typeof BlockId>('BlockId')).options({
8+
t.prop('id', BlockIdRef).options({
99
title: 'Block ID',
1010
description: 'The ID of the block.',
1111
}),
12-
t.propOpt('seq', t.num.options({format: 'u32'})).options({
12+
t.propOpt('cur', BlockCurRef).options({
1313
title: 'Starting Sequence Number',
1414
description: 'The sequence number to start from. Defaults to the latest sequence number.',
1515
}),
@@ -20,21 +20,13 @@ export const scan =
2020
'When positive, returns the patches ahead of the starting sequence number. ' +
2121
'When negative, returns the patches behind the starting sequence number.',
2222
}),
23-
t.propOpt('model', t.bool).options({
24-
title: 'With Model',
25-
intro: 'Whether to include the model.',
26-
description:
27-
'Whether to include the model in the response. ' +
28-
'Defaults to `false`, when `len` is positive; and, defaults to `true`, when `len` is negative.',
29-
}),
3023
);
3124

3225
const Response = t.Object(
33-
t.prop('patches', t.Array(t.Ref<typeof BlockPatch>('BlockPatch'))).options({
26+
t.prop('patches', t.Array(BlockPatchRef)).options({
3427
title: 'Patches',
3528
description: 'The list of patches.',
3629
}),
37-
t.propOpt('model', t.bin),
3830
);
3931

4032
const Func = t.Function(Request, Response).options({
@@ -43,9 +35,11 @@ export const scan =
4335
description: 'Returns a list of specified change patches for a block.',
4436
});
4537

46-
return r.prop('block.scan', Func, async ({id, seq, limit = 10, model: returnModel = limit > 0}) => {
47-
const {patches, model} = await services.blocks.scan(id, seq, limit, returnModel);
48-
const modelBlob: Uint8Array | undefined = model?.toBinary();
49-
return {patches, model: modelBlob};
38+
return r.prop('block.scan', Func, async ({id, cur, limit = 10}) => {
39+
const {patches} = await services.blocks.scan(id, cur, limit);
40+
return {patches: patches.map(p => ({
41+
blob: p.blob,
42+
ts: p.created,
43+
}))};
5044
});
5145
};

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export class BlocksServices {
9393
id: string,
9494
offset: number | undefined,
9595
limit: number | undefined = 10,
96-
returnStartSnapshot: boolean = limit < 0,
9796
) {
9897
const {store} = this;
9998
if (typeof offset !== 'number') offset = await store.seq(id);
@@ -112,14 +111,7 @@ export class BlocksServices {
112111
max = Math.abs(limit);
113112
}
114113
const patches = await store.history(id, min, max);
115-
let model: Model | undefined;
116-
if (returnStartSnapshot) {
117-
const startPatches = await store.history(id, 0, min);
118-
if (startPatches.length) {
119-
model = Model.fromPatches(startPatches.map((p) => Patch.fromBinary(p.blob)));
120-
}
121-
}
122-
return {patches, model};
114+
return {patches};
123115
}
124116

125117
public async edit(id: string, patches: Pick<StorePatch, 'blob'>[]) {

src/__tests__/json-crdt-server/block.ts

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -323,68 +323,61 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
323323
// });
324324
// }
325325

326-
// describe('block.scan', () => {
327-
// test('can retrieve change history', async () => {
328-
// const {call, stop} = await setup();
329-
// const id = getId();
330-
// const model = Model.withLogicalClock();
331-
// model.api.root({
332-
// text: 'Hell',
333-
// });
334-
// const patch1 = model.api.flush();
335-
// await call('block.new', {
336-
// id,
337-
// patches: [
338-
// {
339-
// blob: patch1.toBinary(),
340-
// },
341-
// ],
342-
// });
343-
// await tick(11);
344-
// model.api.str(['text']).ins(4, 'o');
345-
// const patch2 = model.api.flush();
346-
// model.api.obj([]).set({
347-
// age: 26,
348-
// });
349-
// const patch3 = model.api.flush();
350-
// await call('block.upd', {
351-
// id,
352-
// patches: [
353-
// {
354-
// seq: 1,
355-
// created: Date.now(),
356-
// blob: patch2.toBinary(),
357-
// },
358-
// {
359-
// seq: 2,
360-
// created: Date.now(),
361-
// blob: patch3.toBinary(),
362-
// },
363-
// ],
364-
// });
365-
// const history = await call('block.scan', {id, seq: 0, limit: 3});
366-
// expect(history).toMatchObject({
367-
// patches: [
368-
// {
369-
// seq: 0,
370-
// created: expect.any(Number),
371-
// blob: patch1.toBinary(),
372-
// },
373-
// {
374-
// seq: 1,
375-
// created: expect.any(Number),
376-
// blob: patch2.toBinary(),
377-
// },
378-
// {
379-
// seq: 2,
380-
// created: expect.any(Number),
381-
// blob: patch3.toBinary(),
382-
// },
383-
// ],
384-
// });
385-
// stop();
386-
// });
387-
// });
326+
describe('block.scan', () => {
327+
test('can retrieve change history', async () => {
328+
const {call, stop} = await setup();
329+
const id = getId();
330+
const model = Model.withLogicalClock();
331+
model.api.root({
332+
text: 'Hell',
333+
});
334+
const patch1 = model.api.flush();
335+
await call('block.new', {
336+
id,
337+
patches: [
338+
{
339+
blob: patch1.toBinary(),
340+
},
341+
],
342+
});
343+
await tick(11);
344+
model.api.str(['text']).ins(4, 'o');
345+
const patch2 = model.api.flush();
346+
model.api.obj([]).set({
347+
age: 26,
348+
});
349+
const patch3 = model.api.flush();
350+
await call('block.upd', {
351+
id,
352+
patches: [
353+
{
354+
blob: patch2.toBinary(),
355+
},
356+
{
357+
blob: patch3.toBinary(),
358+
},
359+
],
360+
});
361+
const history = await call('block.scan', {id, cur: 0, limit: 3});
362+
expect(history).toMatchObject({
363+
patches: [
364+
{
365+
ts: expect.any(Number),
366+
blob: patch1.toBinary(),
367+
},
368+
{
369+
ts: expect.any(Number),
370+
blob: patch2.toBinary(),
371+
},
372+
{
373+
ts: expect.any(Number),
374+
blob: patch3.toBinary(),
375+
},
376+
],
377+
});
378+
stop();
379+
});
380+
});
388381

389382
describe('block.get', () => {
390383
test('can load a block', async () => {

0 commit comments

Comments
 (0)