Skip to content

Commit

Permalink
feat: add API to change sync auto-stop timeout
Browse files Browse the repository at this point in the history
See [#746].

[#746]: #746
  • Loading branch information
EvanHahn committed Aug 13, 2024
1 parent 241f2a7 commit ce1cdbd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,7 @@ export class SyncApi extends TypedEmitter {
start({ autostopDataSyncAfter = null } = {}) {
assertAutostopDataSyncAfterIsValid(autostopDataSyncAfter)
this.#wantsToSyncData = true
this.#autostopDataSyncAfter = autostopDataSyncAfter
// Ensure the timeout is started anew.
this.#clearAutostopDataSyncTimeoutIfExists()
this.#updateState()
this.setAutostopDataSyncTimeout(autostopDataSyncAfter)
}

/**
Expand Down Expand Up @@ -279,6 +276,17 @@ export class SyncApi extends TypedEmitter {
this.#updateState()
}

/**
* @param {null | number} autostopDataSyncAfter
* @returns {void}
*/
setAutostopDataSyncTimeout(autostopDataSyncAfter) {
assertAutostopDataSyncAfterIsValid(autostopDataSyncAfter)
this.#clearAutostopDataSyncTimeoutIfExists()
this.#autostopDataSyncAfter = autostopDataSyncAfter
this.#updateState()
}

/**
* @param {SyncType} type
* @returns {Promise<void>}
Expand Down
57 changes: 56 additions & 1 deletion test-e2e/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ test('auto-stop', async (t) => {
"invitee hasn't auto-stopped yet because the timer has been restarted"
)

const invitorProjectOnSyncDisabled = pEvent(
let invitorProjectOnSyncDisabled = pEvent(
invitorProject.$sync,
'sync-state',
({ data: { isSyncEnabled } }) => !isSyncEnabled
Expand All @@ -327,6 +327,58 @@ test('auto-stop', async (t) => {
!inviteeProject.$sync.getState().data.isSyncEnabled,
'invitee has auto-stopped'
)

invitorProject.$sync.setAutostopDataSyncTimeout(20_000)
assert(
!invitorProject.$sync.getState().data.isSyncEnabled,
'invitor is still stopped'
)

invitorProject.$sync.start()

const observation2 = await invitorProject.observation.create(
valueOf(generatedObservations[0])
)
await waitForSync(projects, 'full')
assert(
await inviteeProject.observation.getByDocId(observation2.docId),
'invitee receives doc'
)

await clock.tickAsync(19_000)

assert(
invitorProject.$sync.getState().data.isSyncEnabled,
"invitor hasn't auto-stopped"
)

invitorProjectOnSyncDisabled = pEvent(
invitorProject.$sync,
'sync-state',
({ data: { isSyncEnabled } }) => !isSyncEnabled
)

clock.tick(2000)

assert(
!invitorProject.$sync.getState().data.isSyncEnabled,
'invitor has auto-stopped'
)

invitorProject.$sync.start({ autostopDataSyncAfter: 999_999 })
invitorProject.$sync.setAutostopDataSyncTimeout(20_000)

assert(
invitorProject.$sync.getState().data.isSyncEnabled,
'invitor has not yet auto-stopped'
)

await clock.tickAsync(21_000)

assert(
!invitorProject.$sync.getState().data.isSyncEnabled,
'invitor has auto-stopped'
)
})

test('validates auto-stop timeouts', async (t) => {
Expand All @@ -339,6 +391,9 @@ test('validates auto-stop timeouts', async (t) => {
assert.throws(() => {
project.$sync.start({ autostopDataSyncAfter })
})
assert.throws(() => {
project.$sync.setAutostopDataSyncTimeout({ autostopDataSyncAfter })
})
}

assert(!project.$sync.getState().data.isSyncEnabled, 'sync is not enabled')
Expand Down

0 comments on commit ce1cdbd

Please sign in to comment.