From 5f575521f72f40da4e970f219027e71c23281f48 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 26 Nov 2024 17:48:05 +0100 Subject: [PATCH] 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) --- .../authentication/services/AuthService.ts | 22 +++++++++++++++++++ src/pouchdb/getLinks.ts | 8 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) 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 73f4a1535..cbe4e3a38 100644 --- a/src/pouchdb/getLinks.ts +++ b/src/pouchdb/getLinks.ts @@ -12,6 +12,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', @@ -37,7 +40,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(