Skip to content

Commit e976602

Browse files
committed
style: 💄 run Prettier
1 parent 1377676 commit e976602

File tree

31 files changed

+421
-309
lines changed

31 files changed

+421
-309
lines changed

src/__demos__/json-crdt-server/__tests__/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const setupLevelMemory = async () => {
4040
};
4141

4242
export const setupLevelClassic = async () => {
43-
const kv = new ClassicLevel<string, Uint8Array>('./db', { valueEncoding: 'view' })
43+
const kv = new ClassicLevel<string, Uint8Array>('./db', {valueEncoding: 'view'});
4444
await kv.open();
4545
const store = new LevelStore(<any>kv);
4646
return setup(store, async () => kv.close());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const pull =
2525
description: 'Whether to create a new block if it does not exist.',
2626
}),
2727
);
28-
28+
2929
// prettier-ignore
3030
const Response = t.Object(
3131
t.prop('batches', t.Array(BlockBatchRef)).options({

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import {ResolveType} from 'json-joy/lib/json-type';
2-
import {BlockBatchPartialRef, BlockBatchPartialReturnRef, BlockBatchRef, BlockCurRef, BlockIdRef, BlockSnapshotRef} from '../schema';
2+
import {
3+
BlockBatchPartialRef,
4+
BlockBatchPartialReturnRef,
5+
BlockBatchRef,
6+
BlockCurRef,
7+
BlockIdRef,
8+
BlockSnapshotRef,
9+
} from '../schema';
310
import type {RouteDeps, Router, RouterBase} from '../../types';
411

512
export const upd =
@@ -20,7 +27,8 @@ export const upd =
2027
}),
2128
t.propOpt('seq', BlockCurRef).options({
2229
title: 'Sequence Number',
23-
description: 'The last client known sequence number. The server will return history starting from this sequence number.',
30+
description:
31+
'The last client known sequence number. The server will return history starting from this sequence number.',
2432
}),
2533
);
2634

@@ -29,16 +37,20 @@ export const upd =
2937
title: 'Committed Batch Parts',
3038
description: 'Parts of committed batch which were generated on the server.',
3139
}),
32-
t.propOpt('pull', t.Object(
33-
t.prop('batches', t.Array(BlockBatchRef)).options({
34-
title: 'Batches',
35-
description: 'The list of batches that happened after the given sequence number.',
36-
}),
37-
t.propOpt('snapshot', BlockSnapshotRef).options({
38-
title: 'Snapshot',
39-
description: 'The snapshot of the block, to which the batches can be applied to get the current state of the block.',
40-
}),
41-
)),
40+
t.propOpt(
41+
'pull',
42+
t.Object(
43+
t.prop('batches', t.Array(BlockBatchRef)).options({
44+
title: 'Batches',
45+
description: 'The list of batches that happened after the given sequence number.',
46+
}),
47+
t.propOpt('snapshot', BlockSnapshotRef).options({
48+
title: 'Snapshot',
49+
description:
50+
'The snapshot of the block, to which the batches can be applied to get the current state of the block.',
51+
}),
52+
),
53+
),
4254
);
4355

4456
const Func = t.Function(Request, Response).options({
@@ -61,7 +73,7 @@ export const upd =
6173
if (typeof seq === 'number') {
6274
const diff = res.batch.seq - seq;
6375
if (diff <= 1) {
64-
pull = { batches: [] };
76+
pull = {batches: []};
6577
} else {
6678
const needsSnapshot = diff > 100;
6779
let min: number, max: number, limit: number;
@@ -74,7 +86,7 @@ export const upd =
7486
max = res.batch.seq - 1;
7587
limit = max - min + 1;
7688
}
77-
pull = await blocks.scan(id, needsSnapshot, min, limit) as Pull;
89+
pull = (await blocks.scan(id, needsSnapshot, min, limit)) as Pull;
7890
}
7991
}
8092
if (pull) response.pull = pull;

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,25 @@ export const BlockBatchRef = t.Ref<typeof BlockBatch>('BlockBatch');
8282

8383
// ------------------------------------------------------------------- Snapshot
8484

85-
export const BlockSnapshotReturn = t.Object(
86-
t.prop('id', BlockIdRef).options({
87-
title: 'Block ID',
88-
description: 'The ID of the block.',
89-
}),
90-
t.prop('seq', BlockCurRef).options({
91-
title: 'Snapshot Cursor',
92-
description: 'The cursor of the snapshot, representing the position in the history.',
93-
}),
94-
t.prop('ts', t.num.options({format: 'u'})).options({
95-
title: 'Snapshot Creation Time',
96-
description: 'The time when the snapshot was created, in milliseconds since the Unix epoch.',
97-
}),
98-
).options({
99-
title: 'Block Snapshot Return',
100-
description: "Partial snapshot returned on creation, doesn't include the blob.",
101-
});
85+
export const BlockSnapshotReturn = t
86+
.Object(
87+
t.prop('id', BlockIdRef).options({
88+
title: 'Block ID',
89+
description: 'The ID of the block.',
90+
}),
91+
t.prop('seq', BlockCurRef).options({
92+
title: 'Snapshot Cursor',
93+
description: 'The cursor of the snapshot, representing the position in the history.',
94+
}),
95+
t.prop('ts', t.num.options({format: 'u'})).options({
96+
title: 'Snapshot Creation Time',
97+
description: 'The time when the snapshot was created, in milliseconds since the Unix epoch.',
98+
}),
99+
)
100+
.options({
101+
title: 'Block Snapshot Return',
102+
description: "Partial snapshot returned on creation, doesn't include the blob.",
103+
});
102104
export const BlockSnapshotReturnRef = t.Ref<typeof BlockSnapshotReturn>('BlockSnapshotReturn');
103105

104106
// prettier-ignore

src/__demos__/json-crdt-server/services/Services.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {MemoryStore} from './blocks/store/MemoryStore';
55
import {Store} from './blocks/store/types';
66

77
export interface ServicesOpts {
8-
store?: Store,
8+
store?: Store;
99
blocks?: BlocksServicesOpts;
1010
}
1111

@@ -14,12 +14,7 @@ export class Services {
1414
public readonly presence: PresenceService;
1515
public readonly blocks: BlocksServices;
1616

17-
constructor(
18-
{
19-
store = new MemoryStore(),
20-
blocks,
21-
}: ServicesOpts = {}
22-
) {
17+
constructor({store = new MemoryStore(), blocks}: ServicesOpts = {}) {
2318
this.pubsub = new PubsubService();
2419
this.presence = new PresenceService();
2520
this.blocks = new BlocksServices(this, store, blocks);

src/__demos__/json-crdt-server/services/__tests__/BlocksService.spec.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const setup = async (opts?: ServicesOpts) => {
1414
services,
1515
genId,
1616
id,
17-
}
17+
};
1818
};
1919

2020
const setupMemoryLevel = async (opts?: Omit<ServicesOpts, 'store'>) => {
@@ -38,10 +38,10 @@ const run = (setup: Setup) => {
3838
expect(res).toMatchObject({
3939
block: {
4040
id: kit.id,
41-
}
41+
},
4242
});
4343
});
44-
44+
4545
describe('history compaction', () => {
4646
test('can generate a long history', async () => {
4747
const kit = await setup();
@@ -51,20 +51,22 @@ const run = (setup: Setup) => {
5151
for (let i = 0; i < 100; i++) {
5252
model.api.obj([]).set({x: i});
5353
const patch = model.api.flush();
54-
await kit.services.blocks.edit(kit.id, {
55-
patches: [{blob: patch.toBinary()}],
56-
}, false);
54+
await kit.services.blocks.edit(
55+
kit.id,
56+
{
57+
patches: [{blob: patch.toBinary()}],
58+
},
59+
false,
60+
);
5761
}
5862
const {batches, snapshot} = await kit.services.blocks.scan(kit.id, true, 0, 1000);
5963
expect(batches.length).toBe(101);
6064
expect(snapshot!.seq).toBe(-1);
6165
const model2 = Model.fromBinary(snapshot!.blob);
62-
for (const batch of batches)
63-
for (const patch of batch.patches)
64-
model2.applyPatch(Patch.fromBinary(patch.blob));
66+
for (const batch of batches) for (const patch of batch.patches) model2.applyPatch(Patch.fromBinary(patch.blob));
6567
expect(model2.view()).toEqual({foo: 'bar', x: 99});
6668
});
67-
69+
6870
test('can compact history', async () => {
6971
const kit = await setup({
7072
blocks: {
@@ -78,22 +80,24 @@ const run = (setup: Setup) => {
7880
for (let i = 0; i < 100; i++) {
7981
model.api.obj([]).set({x: i});
8082
const patch = model.api.flush();
81-
await kit.services.blocks.edit(kit.id, {
82-
patches: [{blob: patch.toBinary()}],
83-
}, false);
83+
await kit.services.blocks.edit(
84+
kit.id,
85+
{
86+
patches: [{blob: patch.toBinary()}],
87+
},
88+
false,
89+
);
8490
}
8591
await until(async () => (await kit.services.blocks.scan(kit.id, true, 0, 1000)).batches.length === 10);
8692
const {batches, snapshot} = await kit.services.blocks.scan(kit.id, true, 0, 1000);
8793
expect(batches.length).toBe(10);
8894
expect(snapshot!.seq).toBe(90);
8995
const model2 = Model.fromBinary(snapshot!.blob);
90-
for (const batch of batches)
91-
for (const patch of batch.patches)
92-
model2.applyPatch(Patch.fromBinary(patch.blob));
96+
for (const batch of batches) for (const patch of batch.patches) model2.applyPatch(Patch.fromBinary(patch.blob));
9397
expect(model2.view()).toEqual({foo: 'bar', x: 99});
9498
});
9599
});
96-
100+
97101
describe('GC - space reclaim', () => {
98102
test('deletes oldest blocks when GC is called', async () => {
99103
const blocksToDelete = {num: 0};
@@ -132,8 +136,8 @@ const run = (setup: Setup) => {
132136
await tick(2);
133137
blocksToDelete.num = 2;
134138
await create(ids[4]);
135-
await until(async () => await exists(ids[0]) === false);
136-
await until(async () => await exists(ids[1]) === false);
139+
await until(async () => (await exists(ids[0])) === false);
140+
await until(async () => (await exists(ids[1])) === false);
137141
expect(await exists(ids[0])).toBe(false);
138142
expect(await exists(ids[1])).toBe(false);
139143
expect(await exists(ids[2])).toBe(true);
@@ -142,7 +146,7 @@ const run = (setup: Setup) => {
142146
await tick(2);
143147
blocksToDelete.num = 1;
144148
await create(ids[5]);
145-
await until(async () => await exists(ids[2]) === false);
149+
await until(async () => (await exists(ids[2])) === false);
146150
expect(await exists(ids[0])).toBe(false);
147151
expect(await exists(ids[1])).toBe(false);
148152
expect(await exists(ids[2])).toBe(false);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class BlocksServices {
5050
protected readonly store: Store = new MemoryStore(),
5151
protected readonly opts: BlocksServicesOpts = {
5252
historyPerBlock: 10000,
53-
historyCompactionDecision: (seq, pushSize) => ((pushSize > 250) || !(seq % 100)),
53+
historyCompactionDecision: (seq, pushSize) => pushSize > 250 || !(seq % 100),
5454
},
5555
) {
5656
this.spaceReclaimDecision = opts.spaceReclaimDecision ?? storageSpaceReclaimDecision(fs.promises);
@@ -167,9 +167,14 @@ export class BlocksServices {
167167
return {batches};
168168
}
169169

170-
public async pull(id: string, lastKnownSeq: number, create: boolean = false): Promise<{batches: StoreBatch[], snapshot?: StoreSnapshot}> {
170+
public async pull(
171+
id: string,
172+
lastKnownSeq: number,
173+
create: boolean = false,
174+
): Promise<{batches: StoreBatch[]; snapshot?: StoreSnapshot}> {
171175
const {store} = this;
172-
if (typeof lastKnownSeq !== 'number' || lastKnownSeq !== Math.round(lastKnownSeq) || lastKnownSeq < -1) throw RpcError.validation('INVALID_SEQ');
176+
if (typeof lastKnownSeq !== 'number' || lastKnownSeq !== Math.round(lastKnownSeq) || lastKnownSeq < -1)
177+
throw RpcError.validation('INVALID_SEQ');
173178
const seq = await store.seq(id);
174179
if (seq === undefined) {
175180
if (create) {
@@ -214,7 +219,7 @@ export class BlocksServices {
214219
};
215220
const res = await store.push(newSnapshot, batch);
216221
const opts = this.opts;
217-
if ((seq > opts.historyPerBlock) && store.compact && opts.historyCompactionDecision(seq, blobSize)) {
222+
if (seq > opts.historyPerBlock && store.compact && opts.historyCompactionDecision(seq, blobSize)) {
218223
go(() => this.compact(id, seq - opts.historyPerBlock));
219224
}
220225
this.__emitUpd(id, res.batch);
@@ -231,8 +236,7 @@ export class BlocksServices {
231236
await store.compact!(id, to, async (blob, iterator) => {
232237
const model = Model.fromBinary(blob);
233238
for await (const batch of iterator)
234-
for (const patch of batch.patches)
235-
model.applyPatch(Patch.fromBinary(patch.blob));
239+
for (const patch of batch.patches) model.applyPatch(Patch.fromBinary(patch.blob));
236240
return model.toBinary();
237241
});
238242
}

src/__demos__/json-crdt-server/services/blocks/__tests__/util.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {storageSpaceReclaimDecision} from '../util';
22

33
test('returns 0 when more than 300 MB available', async () => {
44
const promises = {
5-
statfs: async () => ({bavail: 400 * 1024, bsize: 1024} as any),
5+
statfs: async () => ({bavail: 400 * 1024, bsize: 1024}) as any,
66
};
77
const fn = storageSpaceReclaimDecision(promises, () => true, 300 * 1024 * 1024);
88
const res = await fn();
@@ -11,7 +11,7 @@ test('returns 0 when more than 300 MB available', async () => {
1111

1212
test('returns greater than 0 when less than 300 MB available', async () => {
1313
const promises = {
14-
statfs: async () => ({bavail: 200 * 1024, bsize: 1024} as any),
14+
statfs: async () => ({bavail: 200 * 1024, bsize: 1024}) as any,
1515
};
1616
const fn = storageSpaceReclaimDecision(promises, () => true, 300 * 1024 * 1024);
1717
const res = await fn();
@@ -20,7 +20,7 @@ test('returns greater than 0 when less than 300 MB available', async () => {
2020

2121
test('returns 0 when not good time for GC', async () => {
2222
const promises = {
23-
statfs: async () => ({bavail: 200 * 1024, bsize: 1024} as any),
23+
statfs: async () => ({bavail: 200 * 1024, bsize: 1024}) as any,
2424
};
2525
const fn = storageSpaceReclaimDecision(promises, () => false, 300 * 1024 * 1024);
2626
const res = await fn();

src/__demos__/json-crdt-server/services/blocks/store/MemoryStore.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class MemoryBlock {
88
constructor(
99
public readonly start: types.StoreSnapshot,
1010
public readonly data: types.StoreBlock,
11-
public readonly history: types.StoreBatch[]
11+
public readonly history: types.StoreBatch[],
1212
) {}
1313
}
1414

@@ -22,7 +22,10 @@ export class MemoryStore implements types.Store {
2222
return {block: block.data};
2323
}
2424

25-
public async getSnapshot(id: string, seq: number): Promise<{snapshot: types.StoreSnapshot, batches: types.StoreBatch[]}> {
25+
public async getSnapshot(
26+
id: string,
27+
seq: number,
28+
): Promise<{snapshot: types.StoreSnapshot; batches: types.StoreBatch[]}> {
2629
await tick;
2730
const block = this.blocks.get(id);
2831
if (!block) throw RpcError.notFound();
@@ -113,7 +116,7 @@ export class MemoryStore implements types.Store {
113116
const batches = block.history;
114117
const length = batches.length;
115118
let i = 0;
116-
async function * iterator() {
119+
async function* iterator() {
117120
for (; i < length; i++) {
118121
const batch = batches[i];
119122
const seq = batch.seq;
@@ -168,11 +171,12 @@ export class MemoryStore implements types.Store {
168171
public async removeAccessedBefore(ts: number, limit = 10): Promise<void> {
169172
await tick;
170173
let cnt = 0;
171-
for (const [id, block] of this.blocks) if (block.data.uts < ts) {
172-
this.removeSync(id);
173-
cnt++;
174-
if (cnt >= limit) return;
175-
}
174+
for (const [id, block] of this.blocks)
175+
if (block.data.uts < ts) {
176+
this.removeSync(id);
177+
cnt++;
178+
if (cnt >= limit) return;
179+
}
176180
}
177181

178182
public async removeOldest(x: number): Promise<void> {

0 commit comments

Comments
 (0)