Skip to content

Commit 7709dfc

Browse files
Rename inbound sync API
1 parent 0dfc44a commit 7709dfc

5 files changed

Lines changed: 132 additions & 128 deletions

File tree

examples/playground/src/playground/hooks/usePlaygroundLiveSubscriptions.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type SetStateAction,
88
} from 'react';
99
import type { Operation } from '@treecrdt/interface';
10-
import { createScopeController, type ScopeController, type SyncScope } from '@treecrdt/sync';
10+
import { createInboundSync, type InboundSync, type SyncScope } from '@treecrdt/sync';
1111
import type { Filter, SyncMessage, SyncPeer } from '@treecrdt/sync-protocol';
1212
import type { DuplexTransport } from '@treecrdt/sync-protocol/transport';
1313

@@ -34,8 +34,8 @@ export function usePlaygroundLiveSubscriptions(opts: {
3434
const [liveAllEnabled, setLiveAllEnabled] = useState(false);
3535
const liveChildrenParentsRef = useRef<Set<string>>(new Set());
3636
const liveAllEnabledRef = useRef(false);
37-
const scopeControllerRef = useRef<ScopeController<Operation> | null>(null);
38-
const scopeControllerPeerRef = useRef<SyncPeer<Operation> | null>(null);
37+
const inboundSyncRef = useRef<InboundSync<Operation> | null>(null);
38+
const inboundSyncPeerRef = useRef<SyncPeer<Operation> | null>(null);
3939
const liveAllScopeRef = useRef<SyncScope | null>(null);
4040
const liveChildScopesRef = useRef<Map<string, SyncScope>>(new Map());
4141
const liveBusyCountRef = useRef(0);
@@ -50,19 +50,19 @@ export function usePlaygroundLiveSubscriptions(opts: {
5050
setLiveBusy(liveBusyCountRef.current > 0);
5151
};
5252

53-
const ensureScopeController = () => {
53+
const ensureInboundSync = () => {
5454
const peer = syncPeerRef.current;
5555
if (!peer) return null;
56-
if (scopeControllerRef.current && scopeControllerPeerRef.current === peer) {
57-
return scopeControllerRef.current;
56+
if (inboundSyncRef.current && inboundSyncPeerRef.current === peer) {
57+
return inboundSyncRef.current;
5858
}
5959

60-
scopeControllerRef.current?.close();
60+
inboundSyncRef.current?.close();
6161
liveAllScopeRef.current = null;
6262
liveChildScopesRef.current.clear();
6363

64-
const controller = createScopeController<Operation>({
65-
peer,
64+
const inbound = createInboundSync<Operation>({
65+
localPeer: peer,
6666
subscribeOptions: (peerId) => syncOnceOptionsForPeer(peerId, 1024),
6767
onWorkStart: beginLiveWork,
6868
onWorkEnd: endLiveWork,
@@ -71,15 +71,15 @@ export function usePlaygroundLiveSubscriptions(opts: {
7171
setSyncError(formatSyncError(error));
7272
},
7373
});
74-
scopeControllerRef.current = controller;
75-
scopeControllerPeerRef.current = peer;
76-
return controller;
74+
inboundSyncRef.current = inbound;
75+
inboundSyncPeerRef.current = peer;
76+
return inbound;
7777
};
7878

7979
const ensureLiveAllScope = () => {
80-
const controller = ensureScopeController();
81-
if (!controller) return;
82-
const scope = liveAllScopeRef.current ?? controller.scope({ all: {} });
80+
const inbound = ensureInboundSync();
81+
if (!inbound) return;
82+
const scope = liveAllScopeRef.current ?? inbound.scope({ all: {} });
8383
liveAllScopeRef.current = scope;
8484
scope.startLive();
8585
};
@@ -90,15 +90,15 @@ export function usePlaygroundLiveSubscriptions(opts: {
9090
};
9191

9292
const ensureLiveChildScope = (parentId: string) => {
93-
const controller = ensureScopeController();
94-
if (!controller) return;
93+
const inbound = ensureInboundSync();
94+
if (!inbound) return;
9595
const existing = liveChildScopesRef.current.get(parentId);
9696
if (existing) {
9797
existing.startLive();
9898
return;
9999
}
100100

101-
const scope = controller.scope({ children: { parent: hexToBytes16(parentId) } });
101+
const scope = inbound.scope({ children: { parent: hexToBytes16(parentId) } });
102102
liveChildScopesRef.current.set(parentId, scope);
103103
scope.startLive();
104104
};
@@ -115,15 +115,15 @@ export function usePlaygroundLiveSubscriptions(opts: {
115115
for (const parentId of liveChildrenParentsRef.current) ensureLiveChildScope(parentId);
116116
};
117117

118-
const setLivePeer = (peerId: string, conn: PlaygroundSyncConnection) => {
119-
const controller = ensureScopeController();
120-
if (!controller) return;
121-
controller.setPeer(peerId, conn.transport as DuplexTransport<SyncMessage<Operation>>);
118+
const addLivePeer = (peerId: string, conn: PlaygroundSyncConnection) => {
119+
const inbound = ensureInboundSync();
120+
if (!inbound) return;
121+
inbound.addPeer(peerId, conn.transport as DuplexTransport<SyncMessage<Operation>>);
122122
startDesiredScopes();
123123
};
124124

125-
const deleteLivePeer = (peerId: string) => {
126-
scopeControllerRef.current?.deletePeer(peerId);
125+
const removeLivePeer = (peerId: string) => {
126+
inboundSyncRef.current?.removePeer(peerId);
127127
};
128128

129129
const toggleLiveChildren = (parentId: string) => {
@@ -136,9 +136,9 @@ export function usePlaygroundLiveSubscriptions(opts: {
136136
};
137137

138138
const resetLiveWork = () => {
139-
scopeControllerRef.current?.close();
140-
scopeControllerRef.current = null;
141-
scopeControllerPeerRef.current = null;
139+
inboundSyncRef.current?.close();
140+
inboundSyncRef.current = null;
141+
inboundSyncPeerRef.current = null;
142142
liveAllScopeRef.current = null;
143143
liveChildScopesRef.current.clear();
144144
liveBusyCountRef.current = 0;
@@ -176,8 +176,8 @@ export function usePlaygroundLiveSubscriptions(opts: {
176176
liveAllEnabledRef,
177177
beginLiveWork,
178178
endLiveWork,
179-
setLivePeer,
180-
deleteLivePeer,
179+
addLivePeer,
180+
removeLivePeer,
181181
resetLiveWork,
182182
};
183183
}

examples/playground/src/playground/hooks/usePlaygroundSync.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { Operation } from '@treecrdt/interface';
33
import { bytesToHex } from '@treecrdt/interface/ids';
44
import { SyncPeer, deriveOpRefV0, type Filter, type SyncAuth } from '@treecrdt/sync-protocol';
55
import {
6+
createInboundSync,
67
createOutboundSync,
7-
createScopeController,
8+
type InboundSync,
89
type OutboundSync,
9-
type ScopeController,
1010
} from '@treecrdt/sync';
1111
import { createTreecrdtSyncBackendFromClient } from '@treecrdt/sync-sqlite';
1212
import type {
@@ -191,8 +191,8 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
191191
liveAllEnabledRef,
192192
beginLiveWork,
193193
endLiveWork,
194-
setLivePeer,
195-
deleteLivePeer,
194+
addLivePeer,
195+
removeLivePeer,
196196
resetLiveWork,
197197
} = usePlaygroundLiveSubscriptions({
198198
syncPeerRef,
@@ -231,7 +231,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
231231
}
232232
connections.delete(peerId);
233233
outboundSyncRef.current?.removePeer(peerId);
234-
deleteLivePeer(peerId);
234+
removeLivePeer(peerId);
235235

236236
if (isRemotePeerId(peerId)) setRemotePeer(null);
237237
else removeMeshPeer(peerId);
@@ -279,35 +279,35 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
279279

280280
setSyncBusy(true);
281281
setSyncError(null);
282-
let manualSyncController: ScopeController<Operation> | null = null;
282+
let manualInboundSync: InboundSync<Operation> | null = null;
283283
try {
284284
const targets = selectSyncTargetIds(connections).filter((peerId) => connections.has(peerId));
285-
manualSyncController = createScopeController<Operation>({
286-
peer,
287-
selectSyncPeerIds: () => targets,
288-
runSync: async ({ peer, peerId, transport, filter }) => {
289-
await syncFiltersWithTransport(peer, peerId, transport, [filter], {
285+
manualInboundSync = createInboundSync<Operation>({
286+
localPeer: peer,
287+
selectPeers: () => targets,
288+
runSync: async ({ localPeer, peerId, transport, filter }) => {
289+
await syncFiltersWithTransport(localPeer, peerId, transport, [filter], {
290290
multipleTargets: targets.length > 1,
291291
label,
292292
});
293293
},
294294
onError: ({ peerId, error }) => {
295295
console.error(`${label} failed for peer`, peerId, error);
296-
manualSyncController?.deletePeer(peerId);
296+
manualInboundSync?.removePeer(peerId);
297297
if (isCapabilityRevokedError(error)) return;
298298
dropPeerConnection(peerId);
299299
},
300300
});
301301
for (const [peerId, conn] of connections) {
302-
manualSyncController.setPeer(peerId, conn.transport);
302+
manualInboundSync.addPeer(peerId, conn.transport);
303303
}
304-
for (const filter of filters) await manualSyncController.scope(filter).syncOnce();
304+
for (const filter of filters) await manualInboundSync.scope(filter).syncOnce();
305305
await refreshMeta();
306306
} catch (err) {
307307
console.error(`${label} failed`, err);
308308
setSyncError(formatSyncError(err));
309309
} finally {
310-
manualSyncController?.close();
310+
manualInboundSync?.close();
311311
setSyncBusy(false);
312312
}
313313
};
@@ -601,7 +601,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
601601
}
602602
const conn = connections.get(peerId);
603603
if (!conn) return;
604-
setLivePeer(peerId, conn);
604+
addLivePeer(peerId, conn);
605605
};
606606

607607
const mesh = channel
@@ -636,7 +636,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
636636
onPeerDisconnected: (peerId) => {
637637
connections.delete(peerId);
638638
outboundSync.removePeer(peerId);
639-
deleteLivePeer(peerId);
639+
removeLivePeer(peerId);
640640
removeMeshPeer(peerId);
641641
},
642642
onBroadcastMessage: (data) => {

0 commit comments

Comments
 (0)