Skip to content

Commit

Permalink
feat: Debounce PouchDB replication
Browse files Browse the repository at this point in the history
In #1209 we added CozyPouchLink to CozyClient's instance

By default CozyPouchLink starts replicating all its database directly
after being initialized

This is problematic as this happens during the App's startup and so it
will unnecessary slow the startup process

This is unnecessary because on startup, either the app is offline and
so replication cannot be done, either the app is online and so it will
use the cozy-stack. So replication can be delayed

We want to defer the replication to a short delay after the startup
(for now 30s)
  • Loading branch information
Ldoppea committed Dec 19, 2024
1 parent 41ba2b7 commit f7a60f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/app/domain/authentication/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Linking } from 'react-native'

import type CozyClient from 'cozy-client'
import Minilog from 'cozy-minilog'
import PouchLink from 'cozy-pouch-link'

import { asyncLogoutNoClient } from '/app/domain/authentication/utils/asyncLogoutNoClient'

Expand All @@ -28,8 +29,29 @@ export const handleSupportEmail = (): void => {
}
}

const handleLogin = (): void => {
try {
authLogger.info('Debounce replication')
if (clientInstance === null) throw new Error('No client instance set')

const pouchLink = getPouchLink(clientInstance)
pouchLink?.startReplicationWithDebounce()
} catch (error) {
authLogger.error('Error while handling login', error)
}
}

export const startListening = (client: CozyClient): void => {
authLogger.info('Start listening to cozy-client events')
clientInstance = client
clientInstance.on('revoked', handleTokenError)
clientInstance.on('login', handleLogin)
}

const getPouchLink = (client?: CozyClient): PouchLink | null => {
if (!client) {
return null
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return client.links.find(link => link instanceof PouchLink) || null
}
8 changes: 7 additions & 1 deletion src/pouchdb/getLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { default as PouchLink } from 'cozy-pouch-link'

const log = Minilog('🔗 GetLinks')

export const REPLICATION_DEBOUNCE = 30 * 1000 // 30s
export const REPLICATION_DEBOUNCE_MAX_DELAY = 600 * 1000 // 10min

export const offlineDoctypes = [
// cozy-home
'io.cozy.accounts',
Expand All @@ -34,7 +37,10 @@ export const offlineDoctypes = [
export const getLinks = (): CozyLink[] => {
const pouchLinkOptions = {
doctypes: offlineDoctypes,
initialSync: true,
initialSync: false,
periodicSync: false,
syncDebounceDelayInMs: REPLICATION_DEBOUNCE,
syncDebounceMaxDelayInMs: REPLICATION_DEBOUNCE_MAX_DELAY,
platform: platformReactNative,
ignoreWarmup: true,
doctypesReplicationOptions: Object.fromEntries(
Expand Down

0 comments on commit f7a60f6

Please sign in to comment.