Skip to content

Commit

Permalink
feat: Delay clients synchronize() call 10s after startup
Browse files Browse the repository at this point in the history
Call to `synchronize()` may slow down the startup process but is not
necessary for the app to work

It is here only to update the cozy-settings's OAuth clients list to
display the last connexion to the app

So we chose to delay this call a short delay after the startup (for now
10s)

By doing so, we may fail calling the `synchronize()` method if the user
closes the app less than 10s after opening it, but as this is used only
for display purpose, we chose to consider this as not-critical
  • Loading branch information
Ldoppea committed Dec 20, 2024
1 parent 4662f7d commit e6122a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const {
synchronizeOnInit
} = SynchronizeService

// @ts-expect-error TS see this as a const but we can edit it on tests runtime
SynchronizeService.SYNCHRONIZE_DELAY_IN_MS = 0

jest.mock('cozy-client')

describe('SynchronizeService', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/app/domain/authentication/services/SynchronizeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { saveClient } from '/libs/clientHelpers/persistClient'

export const syncLog = Minilog('📱 SynchronizeService')

export const SYNCHRONIZE_DELAY_IN_MS = 10 * 1000

export const synchronizeDevice = async (client: CozyClient): Promise<void> => {
try {
await client.getStackClient().fetchJSON('POST', '/settings/synchronized')
Expand Down Expand Up @@ -50,6 +52,12 @@ export const checkClientName = async (client: CozyClient): Promise<void> => {
}

export const synchronizeOnInit = async (client: CozyClient): Promise<void> => {
await checkClientName(client)
await synchronizeDevice(client)
return new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setTimeout(async (): Promise<void> => {
await checkClientName(client)
await synchronizeDevice(client)
resolve()
}, SYNCHRONIZE_DELAY_IN_MS)
})
}

0 comments on commit e6122a1

Please sign in to comment.