diff --git a/src/app/domain/authentication/services/AuthService.ts b/src/app/domain/authentication/services/AuthService.ts index 10c38fc78..58cd35ba1 100644 --- a/src/app/domain/authentication/services/AuthService.ts +++ b/src/app/domain/authentication/services/AuthService.ts @@ -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' @@ -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 } diff --git a/src/pouchdb/getLinks.ts b/src/pouchdb/getLinks.ts index 8b364cff9..4c4aad766 100644 --- a/src/pouchdb/getLinks.ts +++ b/src/pouchdb/getLinks.ts @@ -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', @@ -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(