Skip to content

Commit 671c904

Browse files
committed
feat: 🎸 allow local ops to be applied using .applyPatch()
1 parent ef6c389 commit 671c904

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/json-crdt-repo/JsonCrdtRepo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface JsonCrdtRepoOpts {
1818
export class JsonCrdtRepo {
1919
public readonly sessions: EditSessionFactory;
2020
public readonly opts: JsonCrdtRepoOpts;
21+
public readonly remote: DemoServerRemoteHistory;
2122

2223
constructor(opts: Partial<JsonCrdtRepoOpts>) {
2324
this.opts = {
@@ -26,7 +27,7 @@ export class JsonCrdtRepo {
2627
...opts,
2728
};
2829
const client = createBinaryWsRpcClient(this.opts.wsUrl) as DemoServerClient;
29-
const remote = new DemoServerRemoteHistory(client);
30+
this.remote = new DemoServerRemoteHistory(client);
3031
const kv: BinStrLevel = new BrowserLevel(this.opts.name, {
3132
keyEncoding: 'utf8',
3233
valueEncoding: 'view',
@@ -39,7 +40,7 @@ export class JsonCrdtRepo {
3940
kv,
4041
locks,
4142
sid,
42-
rpc: remote,
43+
rpc: this.remote,
4344
pubsub,
4445
connected$,
4546
});

src/json-crdt-repo/session/EditSession.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ export class EditSession {
3434
protected readonly session: number = Math.floor(Math.random() * 0x7fffffff),
3535
) {
3636
this.log = new Log(() => this.start.clone());
37-
const flushUnsubscribe = this.log.end.api.onFlush.listen((a) => {
37+
const api = this.log.end.api;
38+
const flushUnsubscribe = api.onFlush.listen((a) => {
3839
this.syncLog();
3940
});
41+
const patchUnsubscribe = api.onPatch.listen((patch) => {
42+
const id = patch.getId();
43+
if (!id) return;
44+
const clock = this.model.clock;
45+
if (id.sid === clock.sid && clock.time >= id.time) {
46+
this.syncLog();
47+
}
48+
});
4049
this._stop$.pipe(first()).subscribe(() => {
4150
flushUnsubscribe();
51+
patchUnsubscribe();
4252
});
4353
this.repo.change$(this.id).pipe(takeUntil(this._stop$)).subscribe(this.onEvent);
4454
}
@@ -114,8 +124,10 @@ export class EditSession {
114124

115125
public syncLog(): void {
116126
if (!this.log.patches.size()) return;
117-
this.sync().then((error) => {
118-
this.onsyncerror?.(error);
127+
this._syncRace(() => {
128+
this.sync().then((error) => {
129+
this.onsyncerror?.(error);
130+
});
119131
});
120132
}
121133

src/json-crdt-repo/session/__tests__/EditSession.del.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {s} from 'json-joy/lib/json-crdt';
22
import {setup} from './setup';
3+
import {EditSessionFactory} from '../EditSessionFactory';
34
import {until} from 'thingies/lib/until';
45
import {of} from 'thingies';
5-
import {EditSessionFactory} from '../EditSessionFactory';
66
import {BehaviorSubject} from 'rxjs';
77
import {Testbed} from '../../__tests__/testbed';
88

0 commit comments

Comments
 (0)