Skip to content

Commit fdf7b3b

Browse files
committed
feat: Debounce PouchDB replication
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)
1 parent b00bb13 commit fdf7b3b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/app/domain/authentication/services/AuthService.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Linking } from 'react-native'
22

33
import type CozyClient from 'cozy-client'
44
import Minilog from 'cozy-minilog'
5+
import PouchLink from 'cozy-pouch-link'
56

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

@@ -28,8 +29,29 @@ export const handleSupportEmail = (): void => {
2829
}
2930
}
3031

32+
const handleLogin = (): void => {
33+
try {
34+
authLogger.info('Debounce replication')
35+
if (clientInstance === null) throw new Error('No client instance set')
36+
37+
const pouchLink = getPouchLink(clientInstance)
38+
pouchLink?.startReplicationWithDebounce()
39+
} catch (error) {
40+
authLogger.error('Error while handling login', error)
41+
}
42+
}
43+
3144
export const startListening = (client: CozyClient): void => {
3245
authLogger.info('Start listening to cozy-client events')
3346
clientInstance = client
3447
clientInstance.on('revoked', handleTokenError)
48+
clientInstance.on('login', handleLogin)
49+
}
50+
51+
const getPouchLink = (client?: CozyClient): PouchLink | null => {
52+
if (!client) {
53+
return null
54+
}
55+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
56+
return client.links.find(link => link instanceof PouchLink) || null
3557
}

src/pouchdb/getLinks.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { default as PouchLink } from 'cozy-pouch-link'
1212

1313
const log = Minilog('🔗 GetLinks')
1414

15+
export const REPLICATION_DEBOUNCE = 30 * 1000 // 30s
16+
export const REPLICATION_DEBOUNCE_MAX_DELAY = 600 * 1000 // 10min
17+
1518
export const offlineDoctypes = [
1619
// cozy-home
1720
'io.cozy.accounts',
@@ -37,8 +40,10 @@ export const offlineDoctypes = [
3740
export const getLinks = (): CozyLink[] => {
3841
const pouchLinkOptions = {
3942
doctypes: offlineDoctypes,
40-
initialSync: true,
41-
periodicSync: true,
43+
initialSync: false,
44+
periodicSync: false,
45+
syncDebounceDelayInMs: REPLICATION_DEBOUNCE,
46+
syncDebounceMaxDelayInMs: REPLICATION_DEBOUNCE_MAX_DELAY,
4247
platform: platformReactNative,
4348
ignoreWarmup: true,
4449
doctypesReplicationOptions: Object.fromEntries(

0 commit comments

Comments
 (0)