@@ -24,10 +24,12 @@ import { StateSyncAgent } from './StateSyncAgent';
2424 * - addSyncObserver() is called -> ... to be continued
2525 */
2626
27- type SyncObserver = Observer < HashedObject > ;
27+ type SyncState = { remoteStateHashes : { [ key : Endpoint ] : Hash } , localStateHash ?: Hash , allPeersInSync : boolean , opsToFetch : number , synchronizing : boolean } ;
28+
29+ type SyncObserver = Observer < HashedObject , SyncState > ;
2830type SyncEvent = Event < HashedObject , SyncState > ;
2931
30- type SyncState = { remoteStateHashes : { [ key : Endpoint ] : Hash } , localStateHash ?: Hash , inSync : boolean , opsToFetch : number , synchronizing : boolean }
32+
3133
3234enum SyncObserverEventTypes {
3335 SyncStateUpdate = 'sync-state-update'
@@ -43,7 +45,7 @@ class SyncObserverAgent implements Agent {
4345
4446 pod ?: AgentPod ;
4547
46- relays : Map < AgentId , EventRelay < HashedObject > > ;
48+ relays : Map < AgentId , [ EventRelay < HashedObject > , SyncObserver ] > ;
4749
4850 constructor ( ) {
4951 this . relays = new Map ( ) ;
@@ -59,27 +61,44 @@ class SyncObserverAgent implements Agent {
5961
6062 addSyncObserver ( obs : SyncObserver , mut : MutableObject , peerGroupId : PeerGroupId ) {
6163
64+ console . log ( 'adding sync observer ' , obs , ' for ' , mut . getLastHash ( ) , ' in peer group ' , peerGroupId )
65+
6266 if ( this . pod === undefined ) {
6367 throw new Error ( 'Trying to add a sync observer, but the SyncObserverAgent is not ready.' ) ;
6468 }
6569
6670 const syncAgentId = mut . getSyncAgentId ( peerGroupId ) ;
71+ const syncAgent = this . pod . getAgent ( syncAgentId ) as StateSyncAgent | undefined ;
6772
68- let relay = this . relays . get ( syncAgentId ) ;
73+ let pair = this . relays . get ( syncAgentId ) ;
74+
6975
70- if ( relay === undefined ) {
71- const upstream = new Map < string , EventRelay < HashedObject > > ( ) ;
76+ if ( pair === undefined ) {
7277
73- const syncAgent = this . pod . getAgent ( syncAgentId ) as StateSyncAgent ;
74- if ( syncAgent !== undefined ) {
75- upstream . set ( 'agent' , syncAgent . getSyncEventSource ( ) ) ;
78+ const relay = new EventRelay < HashedObject > ( mut ) ;
79+ const syncAgentObs = ( ev : SyncEvent ) => {
80+ relay . emit ( ev ) ;
7681 }
7782
78- relay = new EventRelay < HashedObject > ( mut , upstream ) ;
79- this . relays . set ( syncAgentId , relay ) ;
83+ pair = [ relay , syncAgentObs ] ;
84+
85+ this . relays . set ( syncAgentId , [ relay , syncAgentObs ] ) ;
86+
87+
88+ if ( syncAgent !== undefined ) {
89+ syncAgent . getSyncEventSource ( ) . addObserver ( syncAgentObs ) ;
90+ }
8091 }
8192
82- relay . addObserver ( obs ) ;
93+ pair [ 0 ] . addObserver ( obs ) ;
94+
95+ if ( syncAgent !== undefined ) {
96+ obs ( {
97+ emitter : mut ,
98+ action : SyncObserverEventTypes . SyncStateUpdate ,
99+ data : syncAgent . getSyncState ( )
100+ } ) ;
101+ }
83102 }
84103
85104 removeSyncObserver ( obs : SyncObserver , mut : MutableObject , peerGroupId : PeerGroupId ) {
@@ -88,13 +107,20 @@ class SyncObserverAgent implements Agent {
88107 }
89108
90109 const syncAgentId = mut . getSyncAgentId ( peerGroupId ) ;
91- let relay = this . relays . get ( syncAgentId ) ;
110+ const pair = this . relays . get ( syncAgentId ) ;
92111
93- if ( relay !== undefined ) {
112+ if ( pair !== undefined ) {
113+ const [ relay , syncAgentObs ] = pair ;
94114 relay . removeObserver ( obs ) ;
95115
96116 if ( relay . observers . size === 0 ) {
97- relay . removeAllUpstreamRelays ( ) ;
117+
118+ const syncAgent = this . pod . getAgent ( syncAgentId ) as StateSyncAgent | undefined ;
119+
120+ if ( syncAgent !== undefined ) {
121+ syncAgent . getSyncEventSource ( ) . removeObserver ( syncAgentObs ) ;
122+ }
123+
98124 this . relays . delete ( syncAgentId ) ;
99125 }
100126 }
@@ -106,14 +132,19 @@ class SyncObserverAgent implements Agent {
106132 const agentEv = ev as AgentSetChangeEvent ;
107133
108134 const syncAgentId = agentEv . content . agentId ;
109- const relay = this . relays . get ( syncAgentId ) ;
135+ const pair = this . relays . get ( syncAgentId ) ;
110136
111- if ( relay !== undefined ) {
137+ if ( pair !== undefined ) {
112138 if ( agentEv . content . change === AgentSetChange . Addition ) {
113139 const syncAgent = this . pod ?. getAgent ( syncAgentId ) as StateSyncAgent ;
114- relay . addUpstreamRelay ( 'agent' , syncAgent . getSyncEventSource ( ) ) ;
140+ const [ _relay , syncAgentObserver ] = pair ;
141+ syncAgent . getSyncEventSource ( ) . addObserver ( syncAgentObserver ) ;
142+ console . log ( '+++ adding agent relay ' , syncAgent . getSyncEventSource ( ) , ' to ' , _relay )
115143 } else if ( agentEv . content . change === AgentSetChange . Removal ) {
116- relay . removeUpstreamRelay ( 'agent' ) ;
144+ const syncAgent = this . pod ?. getAgent ( syncAgentId ) as StateSyncAgent | undefined ;
145+ const [ _relay , syncAgentObserver ] = pair ;
146+ syncAgent ?. getSyncEventSource ( ) . addObserver ( syncAgentObserver ) ;
147+ console . log ( '+++ removing agent relay from ' , _relay )
117148 }
118149
119150 }
0 commit comments