Skip to content

Commit 52b273c

Browse files
committed
feat: 🎸 add sync loop methods
1 parent 128c012 commit 52b273c

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"@jsonjoy.com/json-pack": "^1.0.2",
7676
"@jsonjoy.com/util": "^1.0.0",
7777
"json-joy": "^15.4.1",
78-
"memfs": "^4.9.1",
78+
"memfs": "^5.0.0-next.1",
7979
"sonic-forest": "^1.0.0",
8080
"thingies": "^2.1.0"
8181
},

src/json-crdt-repo/local/server-crud/ServerCrudLocalHistorySync.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,34 @@ export interface ServerCrudLocalHistorySyncOpts {
1010
* Number of milliseconds after which remote calls are considered timed out.
1111
*/
1212
remoteTimeout?: number;
13+
14+
/**
15+
* Minimum backoff time in milliseconds for the sync loop.
16+
*/
17+
syncLoopMinBackoff?: number;
18+
19+
/**
20+
* Maximum backoff time in milliseconds for the sync loop.
21+
*/
22+
syncLoopMaxBackoff?: number;
1323
}
1424

1525
export class ServerCrudLocalHistorySync {
26+
private syncLoopTimer: any = 0;
27+
1628
constructor(
1729
protected readonly opts: ServerCrudLocalHistorySyncOpts,
1830
protected readonly core: ServerCrudLocalHistoryCore,
1931
) {}
2032

33+
public start(): void {
34+
35+
}
36+
37+
public stop(): void {
38+
39+
}
40+
2141
protected remoteTimeout(): number {
2242
return this.opts.remoteTimeout ?? 5000;
2343
}
@@ -146,10 +166,28 @@ export class ServerCrudLocalHistorySync {
146166
}
147167
}
148168

149-
public async * listDirty(collection: string[] = ['sync', 'dirty']): AsyncIterableIterator<{collection: string[]; id: string}> {
169+
protected async * listDirty(collection: string[] = ['sync', 'dirty']): AsyncIterableIterator<{collection: string[]; id: string}> {
150170
for await (const entry of this.core.crud.scan(collection)) {
151171
if (entry.type === 'collection') yield* this.listDirty([...collection, entry.id]);
152172
else yield {collection, id: entry.id};
153173
}
154174
}
175+
176+
protected async * syncDirty(): AsyncIterableIterator<[block: {collection: string[]; id: string}, success: boolean]> {
177+
for await (const block of this.listDirty()) {
178+
const {collection, id} = block;
179+
const success = await this.push(collection, id);
180+
yield [block, success];
181+
}
182+
}
183+
184+
protected async syncAllDirty(): Promise<SyncResultList> {
185+
const list: SyncResultList = [];
186+
for await (const result of this.syncDirty()) list.push(result);
187+
return list;
188+
}
155189
}
190+
191+
export type ItemId = {collection: string[], id: string};
192+
export type SyncResult = [block: ItemId, success: boolean];
193+
export type SyncResultList = SyncResult[];

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,10 +2034,10 @@ marked@^4.3.0:
20342034
resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3"
20352035
integrity sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==
20362036

2037-
memfs@^4.9.1:
2038-
version "4.9.1"
2039-
resolved "https://registry.yarnpkg.com/memfs/-/memfs-4.9.1.tgz#cd0b94987c365298a6e57b4beb98c658008d7ff1"
2040-
integrity sha512-36cVYFMaa9HNEYyvkyKCwker8DBmOdjWLrfekE/cHEKJ806fCfKNVhOJNvoyV/CrGSZDtfQPbhn0Zid0gbH0Hw==
2037+
memfs@^5.0.0-next.1:
2038+
version "5.0.0-next.1"
2039+
resolved "https://registry.yarnpkg.com/memfs/-/memfs-5.0.0-next.1.tgz#fbf85543cbcdda5c2c47a6a20857437f3b8ee29e"
2040+
integrity sha512-SrXXr7KM5P2xfYbHopapF410Ib8LZS7OPsxfUDvayA/pz/NHirxwfUVFInmSOJvjsxrmm2+VStLYb2AbNaMHLA==
20412041
dependencies:
20422042
"@jsonjoy.com/json-pack" "^1.0.2"
20432043
"@jsonjoy.com/util" "^1.1.0"

0 commit comments

Comments
 (0)