Skip to content

Commit 476f64f

Browse files
committed
feat: 🎸 update remote.scanBwd() method
1 parent af459c9 commit 476f64f

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ export class BlocksServices {
106106
if (!limit || Math.round(limit) !== limit) throw RpcError.badRequest('INVALID_LIMIT');
107107
if (limit > 0) {
108108
min = Number(offset) || 0;
109-
max = min + limit;
109+
max = min + limit - 1;
110110
} else {
111111
max = Number(offset) || 0;
112-
min = max - limit;
112+
min = max - limit + 1;
113113
}
114114
if (min < 0) {
115115
min = 0;

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class DemoServerRemoteHistory
3131
const limit = 100;
3232
const res = await this.client.call('block.scan', {
3333
id,
34-
cur: cursor,
34+
cur: cursor + 1,
3535
limit,
3636
});
3737
return res;
@@ -40,14 +40,20 @@ export class DemoServerRemoteHistory
4040
public async scanBwd(
4141
id: string,
4242
cursor: Cursor,
43-
): Promise<{snapshot: DemoServerSnapshot; patches: DemoServerPatch[]}> {
44-
throw new Error('Method not implemented.');
45-
// const res = await this.client.call('block.scan', {
46-
// id,
47-
// seq: cursor,
48-
// limit: -100,
49-
// model: true,
50-
// });
43+
): Promise<{snapshot?: DemoServerSnapshot; patches: DemoServerPatch[]}> {
44+
if (cursor <= 0) {
45+
return {
46+
patches: [],
47+
};
48+
}
49+
const res = await this.client.call('block.scan', {
50+
id,
51+
cur: 0,
52+
limit: cursor,
53+
});
54+
return {
55+
patches: res.patches,
56+
};
5157
}
5258

5359
public async create(

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,43 @@ describe('.scanFwd()', () => {
133133
const patch2 = model1.api.flush();
134134
const blob2 = patch2.toBinary();
135135
await remote.update(id, [{blob: blob2}]);
136-
const scan1 = await remote.scanFwd(id, read1.block.snapshot.cur + 1);
136+
const scan1 = await remote.scanFwd(id, read1.block.snapshot.cur);
137137
expect(scan1).toMatchObject({
138138
patches: [{
139139
blob: expect.any(Uint8Array),
140140
ts: expect.any(Number),
141141
}]
142142
});
143+
expect(scan1.patches[0].blob).toEqual(blob2);
144+
});
145+
});
146+
147+
describe('.scanBwd()', () => {
148+
test('can scan patches backward', async () => {
149+
const {remote} = await setup();
150+
const id = genId();
151+
const model1 = Model.withLogicalClock();
152+
model1.api.root({score: 42});
153+
const patch1 = model1.api.flush();
154+
const blob1 = patch1.toBinary();
155+
await remote.create(id, [{blob: blob1}]);
156+
const read1 = await remote.read(id);
157+
model1.api.obj([]).set({
158+
foo: 'bar',
159+
});
160+
const patch2 = model1.api.flush();
161+
const blob2 = patch2.toBinary();
162+
await remote.update(id, [{blob: blob2}]);
163+
const read2 = await remote.read(id);
164+
const scan1 = await remote.scanBwd(id, read2.block.snapshot.cur);
165+
expect(scan1.patches.length).toBe(1);
166+
expect(scan1).toMatchObject({
167+
patches: [{
168+
blob: expect.any(Uint8Array),
169+
ts: expect.any(Number),
170+
}]
171+
});
172+
expect(scan1.patches[0].blob).toEqual(blob1);
143173
});
144174
});
145175

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface RemoteHistory<
5959
* @param id ID of the block.
6060
* @param cursor The cursor until which to scan.
6161
*/
62-
scanBwd(id: string, cursor: Cursor): Promise<{snapshot: S; patches: P[]}>;
62+
scanBwd(id: string, cursor: Cursor): Promise<{patches: P[]; snapshot?: S}>;
6363

6464
/**
6565
* Create a new block with the given patches.

0 commit comments

Comments
 (0)