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 05e2cb5 commit 53a9aed
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/sync/sync-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ export class SyncApi extends TypedEmitter {
* happens after this duration in milliseconds, sync will be automatically
* stopped as if {@link stop} was called.
*/
start({ autostopDataSyncAfter = null } = {}) {
assertAutostopDataSyncAfterIsValid(autostopDataSyncAfter)
start({ autostopDataSyncAfter } = {}) {
this.#wantsToSyncData = true
this.#autostopDataSyncAfter = autostopDataSyncAfter
// Ensure the timeout is started anew.
this.#clearAutostopDataSyncTimeoutIfExists()
this.#updateState()
if (autostopDataSyncAfter === undefined) {
this.#updateState()
} else {
this.setAutostopDataSyncTimeout(autostopDataSyncAfter)
}
}

/**
Expand Down Expand Up @@ -279,6 +279,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
64 changes: 63 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,65 @@ 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 observation3 = await invitorProject.observation.create(
valueOf(generatedObservations[0])
)
await waitForSync(projects, 'full')
assert(
await inviteeProject.observation.getByDocId(observation3.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(2_000)

await invitorProjectOnSyncDisabled
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'
)

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

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

test('validates auto-stop timeouts', async (t) => {
Expand All @@ -339,6 +398,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 53a9aed

Please sign in to comment.