77 type SetStateAction ,
88} from 'react' ;
99import 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' ;
1111import type { Filter , SyncMessage , SyncPeer } from '@treecrdt/sync-protocol' ;
1212import 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}
0 commit comments