Skip to content

Commit

Permalink
add sync state connectedPeers prop
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Dec 11, 2023
1 parent a7ba505 commit 317101a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const kSyncState = Symbol('sync state')
*/

/**
* @typedef {Record<SyncType, SyncTypeState>} State
* @typedef {object} State
* @property {SyncTypeState} initial State of initial sync (sync of auth, metadata and project config)
* @property {SyncTypeState} data State of data sync (observations, map data, photos, audio, video etc.)
* @property {number} connectedPeers Number of connected peers
*/

/**
Expand Down Expand Up @@ -68,7 +71,7 @@ export class SyncApi extends TypedEmitter {
this[kSyncState].setMaxListeners(0)
this[kSyncState].on('state', (namespaceSyncState) => {
const state = reduceSyncState(namespaceSyncState)
state.full.syncing = this.#dataSyncEnabled.has('local')
state.data.syncing = this.#dataSyncEnabled.has('local')
this.emit('sync-state', state)
})

Expand Down Expand Up @@ -110,7 +113,7 @@ export class SyncApi extends TypedEmitter {
*/
getState() {
const state = reduceSyncState(this[kSyncState].getState())
state.full.syncing = this.#dataSyncEnabled.has('local')
state.data.syncing = this.#dataSyncEnabled.has('local')
return state
}

Expand Down Expand Up @@ -247,17 +250,21 @@ function isSynced(state, namespaces, peerSyncControllers) {
* @returns {State}
*/
function reduceSyncState(namespaceSyncState) {
const connectedPeers = Object.values(
namespaceSyncState.auth.remoteStates
).filter((remoteState) => remoteState.status === 'connected').length
const state = {
initial: createInitialSyncTypeState(),
full: createInitialSyncTypeState(),
data: createInitialSyncTypeState(),
connectedPeers,
}
for (const ns of PRESYNC_NAMESPACES) {
const nsState = namespaceSyncState[ns]
mutatingAddNamespaceState(state.initial, nsState)
}
for (const ns of DATA_NAMESPACES) {
const nsState = namespaceSyncState[ns]
mutatingAddNamespaceState(state.full, nsState)
mutatingAddNamespaceState(state.data, nsState)
}
return state
}
Expand Down

0 comments on commit 317101a

Please sign in to comment.