1- import { PeerGroupAgentConfig , PeerInfo , PeerSource } from '../../agents/peer' ;
2- import { CannotInferPeerGroup , Mesh , SyncMode , UsageToken } from '../../service/Mesh' ;
3- import { SpawnCallback } from '../../agents/spawn' ;
4- import { PeerGroupState } from '../../agents/peer/PeerGroupState' ;
5- import { ObjectDiscoveryReply } from '../../agents/discovery' ;
6- import { Endpoint } from '../../agents/network' ;
7-
81import { Identity , RSAKeyPair } from 'data/identity' ;
92import { Context , HashedObject , LiteralContext , MutableObject } from 'data/model' ;
103import { Hash } from 'data/model' ;
@@ -15,7 +8,14 @@ import { RNGImpl } from 'crypto/random';
158
169import { Store } from 'storage/store' ;
1710import { LinkupAddress } from 'net/linkup' ;
18- import { SyncState } from 'mesh/agents/state' ;
11+
12+ import { PeerGroupAgentConfig , PeerInfo , PeerSource } from '../../agents/peer' ;
13+ import { CannotInferPeerGroup , Mesh , SyncMode , UsageToken } from '../../service/Mesh' ;
14+ import { SpawnCallback } from '../../agents/spawn' ;
15+ import { PeerGroupState } from '../../agents/peer/PeerGroupState' ;
16+ import { ObjectDiscoveryReply } from '../../agents/discovery' ;
17+ import { Endpoint } from '../../agents/network' ;
18+ import { SyncEvent , SyncObserver , SyncState } from '../../agents/state' ;
1919
2020/* Run a mesh remotely, and access it through a MeshProxy */
2121
@@ -31,13 +31,15 @@ import { SyncState } from 'mesh/agents/state';
3131 *
3232 * Ah, the things we do for you, Hyper Hyper Space. */
3333
34+ type ObserverId = string ;
35+
3436type MeshCommand = JoinPeerGroup | LeavePeerGroup | ForwardPeerGroupState |
3537 SyncObjectsWithPeerGroup | StopSyncObjectsWithPeerGroup |
3638 StartObjectBroadcast | StopObjectBroadcast |
3739 FindObjectByHash | FindObjectByHashSuffix |
3840 ForwardGetPeersReply | ForwardGetPeerForEndpointReply |
3941 AddObjectSpawnCallback | SendObjectSpawnRequest |
40- ForwardSyncState |
42+ ForwardSyncState | AddSyncObserver | RemoveSyncObserver |
4143 Shutdown ;
4244
4345type JoinPeerGroup = {
@@ -147,6 +149,20 @@ type ForwardSyncState = {
147149 peerGroupId ?: string
148150}
149151
152+ type AddSyncObserver = {
153+ type : 'add-sync-observer' ,
154+ observerId : ObserverId ,
155+ mutLiteralContext : LiteralContext ,
156+ peerGroupId ?: string
157+ }
158+
159+ type RemoveSyncObserver = {
160+ type : 'remove-sync-observer' ,
161+ observerId : ObserverId ,
162+ mutLiteralContext : LiteralContext ,
163+ peerGroupId ?: string
164+ }
165+
150166type Shutdown = {
151167 type : 'shutdown'
152168}
@@ -167,7 +183,8 @@ type ForwardGetPeerForEndpointReply = {
167183
168184type PeerInfoContext = { endpoint : Endpoint , identityHash : Hash , identity ?: LiteralContext } ;
169185
170- type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback | PeerGroupStateReply | SyncStateReply ;
186+ type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback | PeerGroupStateReply |
187+ SyncStateReply | AddSyncObserverReply | RemoveSyncObserverReply | SyncObserverEventReply ;
171188
172189type LiteralObjectDiscoveryReply = {
173190 type : 'object-discovery-reply'
@@ -210,6 +227,27 @@ type SyncStateReply = {
210227 errorType ?: 'infer-peer-group'
211228}
212229
230+ type AddSyncObserverReply = {
231+ type : 'add-sync-observer-reply' ,
232+ observerId : ObserverId ,
233+ errorType ?: 'infer-peer-group' ,
234+ error ?: string
235+ }
236+
237+ type RemoveSyncObserverReply = {
238+ type : 'remove-sync-observer-reply' ,
239+ observerId : ObserverId ,
240+ errorType ?: 'infer-peer-group' ,
241+ error ?: string
242+ }
243+
244+ type SyncObserverEventReply = {
245+ type : 'sync-observer-event-reply' ,
246+ observerId : ObserverId ,
247+ action : string ,
248+ data : SyncState
249+ }
250+
213251type PeerSourceRequest = GetPeersRequest | GetPeerForEndpointRequest ;
214252
215253type GetPeersRequest = {
@@ -249,17 +287,23 @@ class MeshHost {
249287 type === 'forward-get-peer-for-endpoint-reply' ||
250288 type === 'add-object-spawn-callback' ||
251289 type === 'send-object-spawn-callback' ||
252- type === 'forward-sync-state'
290+ type === 'forward-sync-state' ||
291+ type === 'add-sync-observer' ||
292+ type === 'remove-sync-observer'
253293 ) ;
254294 }
255295
256296 static isStreamedReply ( msg : any ) : boolean {
257297 const type = msg ?. type ;
258298
259299 return ( type === 'object-discovery-reply' ||
260- type === 'object-discovery-end' ||
261- type === 'object-spawn-callback' ||
262- type === 'peer-group-state-reply' ) ;
300+ type === 'object-discovery-end' ||
301+ type === 'object-spawn-callback' ||
302+ type === 'peer-group-state-reply' ||
303+ type === 'sync-state-reply' ||
304+ type === 'add-sync-observer-reply' ||
305+ type === 'remove-sync-observer-reply' ||
306+ type === 'sync-observer-event-reply' ) ;
263307 }
264308
265309 static isPeerSourceRequest ( msg : any ) : boolean {
@@ -280,6 +324,8 @@ class MeshHost {
280324
281325 stores : Map < string , Map < string , Store > > ;
282326
327+ syncObservers : Map < ObserverId , SyncObserver > ;
328+
283329 constructor ( mesh : Mesh , streamedReplyCb : ( resp : CommandStreamedReply ) => void , peerSourceReqCb : ( req : PeerSourceRequest ) => void ) {
284330 this . mesh = mesh ;
285331 this . streamedReplyCb = streamedReplyCb ;
@@ -288,6 +334,7 @@ class MeshHost {
288334 this . pendingPeerForEndpointRequests = new Map ( ) ;
289335 this . spawnCallbacks = new Map ( ) ;
290336 this . stores = new Map ( ) ;
337+ this . syncObservers = new Map ( ) ;
291338 }
292339
293340 execute ( command : MeshCommand ) : void {
@@ -371,11 +418,72 @@ class MeshHost {
371418 reply . errorType = 'infer-peer-group' ;
372419 }
373420
374- reply . error = reason ;
421+ reply . error = reason . toString ( ) ;
375422
376423 this . streamedReplyCb ( reply ) ;
377424 } ) ;
378-
425+
426+ } else if ( command . type === 'add-sync-observer' ) {
427+ let obs : SyncObserver = ( ev : SyncEvent ) => {
428+
429+ const reply : SyncObserverEventReply = {
430+ type : 'sync-observer-event-reply' ,
431+ observerId : command . observerId ,
432+ action : ev . action ,
433+ data : ev . data
434+ }
435+
436+ this . streamedReplyCb ( reply ) ;
437+ }
438+
439+ const reply : AddSyncObserverReply = {
440+ type : 'add-sync-observer-reply' ,
441+ observerId : command . observerId
442+ }
443+
444+ try {
445+ let mut = HashedObject . fromLiteralContext ( command . mutLiteralContext ) as MutableObject ;
446+ this . mesh . addSyncObserver ( obs , mut , command . peerGroupId ) ;
447+ } catch ( e : any ) {
448+ if ( e instanceof CannotInferPeerGroup ) {
449+ reply . errorType = 'infer-peer-group'
450+ }
451+ reply . error = e . toString ( ) ;
452+ }
453+
454+ if ( reply . error === undefined && reply . errorType === undefined ) {
455+ this . syncObservers . set ( command . observerId , obs ) ;
456+ }
457+
458+ this . streamedReplyCb ( reply ) ;
459+
460+ } else if ( command . type === 'remove-sync-observer' ) {
461+
462+ const reply : RemoveSyncObserverReply = {
463+ type : 'remove-sync-observer-reply' ,
464+ observerId : command . observerId
465+ }
466+
467+ try {
468+ const obs = this . syncObservers . get ( command . observerId ) ;
469+
470+ if ( obs !== undefined ) {
471+ let mut = HashedObject . fromLiteralContext ( command . mutLiteralContext ) as MutableObject ;
472+ this . mesh . removeSyncObserver ( obs , mut , command . peerGroupId ) ;
473+ }
474+ } catch ( e : any ) {
475+ if ( e instanceof CannotInferPeerGroup ) {
476+ reply . errorType = 'infer-peer-group'
477+ }
478+ reply . error = e . toString ( ) ;
479+ }
480+
481+ if ( reply . error === undefined && reply . errorType === undefined ) {
482+ this . syncObservers . delete ( command . observerId ) ;
483+ }
484+
485+ this . streamedReplyCb ( reply ) ;
486+
379487 } else if ( command . type === 'sync-objects-with-peer-group' ) {
380488 const syncObjs = command as SyncObjectsWithPeerGroup ;
381489
@@ -675,7 +783,7 @@ export { MeshHost, MeshCommand,
675783 SyncObjectsWithPeerGroup , StopSyncObjectsWithPeerGroup ,
676784 StartObjectBroadcast , StopObjectBroadcast ,
677785 FindObjectByHash , FindObjectByHashSuffix , AddObjectSpawnCallback , SendObjectSpawnRequest ,
678- ForwardSyncState ,
786+ ForwardSyncState , AddSyncObserver , RemoveSyncObserver ,
679787 Shutdown ,
680788 CommandStreamedReply , LiteralObjectDiscoveryReply , DiscoveryEndReply , ObjectSpawnCallback , PeerGroupStateReply , LiteralPeerInfo ,
681789 ForwardGetPeersReply , ForwardGetPeerForEndpointReply ,
0 commit comments