Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emit sync state on sync.{start,stop} #518

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export class SyncApi extends TypedEmitter {
this[kSyncState] = new SyncState({ coreManager, throttleMs })
this[kSyncState].setMaxListeners(0)
this[kSyncState].on('state', (namespaceSyncState) => {
const state = reduceSyncState(namespaceSyncState)
state.data.syncing = this.#dataSyncEnabled.has('local')
const state = this.#getState(namespaceSyncState)
this.emit('sync-state', state)
})

Expand Down Expand Up @@ -112,7 +111,15 @@ export class SyncApi extends TypedEmitter {
* @returns {State}
*/
getState() {
const state = reduceSyncState(this[kSyncState].getState())
return this.#getState(this[kSyncState].getState())
}

/**
* @param {import('./sync-state.js').State} namespaceSyncState
* @returns {State}
*/
#getState(namespaceSyncState) {
const state = reduceSyncState(namespaceSyncState)
state.data.syncing = this.#dataSyncEnabled.has('local')
return state
}
Expand All @@ -127,6 +134,7 @@ export class SyncApi extends TypedEmitter {
for (const peerSyncController of this.#peerSyncControllers.values()) {
peerSyncController.enableDataSync()
}
this.emit('sync-state', this.getState())
}

/**
Expand All @@ -139,6 +147,7 @@ export class SyncApi extends TypedEmitter {
for (const peerSyncController of this.#peerSyncControllers.values()) {
peerSyncController.disableDataSync()
}
this.emit('sync-state', this.getState())
}

/**
Expand Down
38 changes: 38 additions & 0 deletions test-e2e/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { test } from 'brittle'
import { setTimeout as delay } from 'timers/promises'
import {
connectPeers,
createManagers,
Expand Down Expand Up @@ -259,3 +260,40 @@ test('no sync capabilities === no namespaces sync apart from auth', async (t) =>

await Promise.all(projects.map((p) => p.close()))
})

test('Sync state emitted when starting and stopping sync', async function (t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential suggestion: add some assertions to test idempotency of start() and stop() i.e. events are not emitted for subsequent calls that do not meaningfully change sync state

maybe that's diving too much into internals though...

const COUNT = 2
const managers = await createManagers(COUNT, t)
const [invitor, ...invitees] = managers
const projectId = await invitor.createProject({ name: 'Mapeo' })

const disconnect = connectPeers(managers, { discovery: false })

await invite({ invitor, invitees, projectId })

const projects = await Promise.all(
managers.map((m) => m.getProject(projectId))
)

const stateEvents = []

projects[0].$sync.on('sync-state', (state) => {
const timestamp = Date.now()
stateEvents.push({ state, timestamp })
})

projects[0].$sync.start()
t.ok(stateEvents.length === 1, 'sync-state event emitted after start')

await delay(500)

const eventCountBeforeStop = stateEvents.length
projects[0].$sync.stop()
t.ok(
stateEvents.length > eventCountBeforeStop,
'sync-state event emitted after stop'
)

await disconnect()
await Promise.all(projects.map((p) => p.close()))
})
Loading