Skip to content

Commit

Permalink
chore: simplify SyncApi "is syncing" logic (#610)
Browse files Browse the repository at this point in the history
This change should have no impact.

`SyncApi` needs to know whether sync is enabled. Currently, it does this
in a roundabout way: appending a value to a set and checking for that
value.

This change replaces that with a simpler boolean flag.
  • Loading branch information
EvanHahn authored May 6, 2024
1 parent b780898 commit 7ff87a1
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export class SyncApi extends TypedEmitter {
#pscByPeerId = new Map()
/** @type {Set<string>} */
#peerIds = new Set()
/** @type {Set<'local' | 'remote'>} */
#dataSyncEnabled = new Set()
#isSyncing = false
/** @type {Map<import('protomux'), Set<Buffer>>} */
#pendingDiscoveryKeys = new Map()
#l
Expand Down Expand Up @@ -126,16 +125,16 @@ export class SyncApi extends TypedEmitter {
*/
#getState(namespaceSyncState) {
const state = reduceSyncState(namespaceSyncState)
state.data.syncing = this.#dataSyncEnabled.has('local')
state.data.syncing = this.#isSyncing
return state
}

/**
* Start syncing data cores
*/
start() {
if (this.#dataSyncEnabled.has('local')) return
this.#dataSyncEnabled.add('local')
if (this.#isSyncing) return
this.#isSyncing = true
this.#l.log('Starting data sync')
for (const peerSyncController of this.#peerSyncControllers.values()) {
peerSyncController.enableDataSync()
Expand All @@ -147,8 +146,8 @@ export class SyncApi extends TypedEmitter {
* Stop syncing data cores (metadata cores will continue syncing in the background)
*/
stop() {
if (!this.#dataSyncEnabled.has('local')) return
this.#dataSyncEnabled.delete('local')
if (!this.#isSyncing) return
this.#isSyncing = false
this.#l.log('Stopping data sync')
for (const peerSyncController of this.#peerSyncControllers.values()) {
peerSyncController.disableDataSync()
Expand Down Expand Up @@ -207,7 +206,7 @@ export class SyncApi extends TypedEmitter {
// Add peer to all core states (via namespace sync states)
this[kSyncState].addPeer(peerSyncController.peerId)

if (this.#dataSyncEnabled.has('local')) {
if (this.#isSyncing) {
peerSyncController.enableDataSync()
}

Expand Down

0 comments on commit 7ff87a1

Please sign in to comment.