Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize startup timings by delaying heavy tasts #1266

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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
@@ -1,4 +1,4 @@
import Minilog from 'cozy-minilog'

Check warning on line 1 in src/app/domain/authentication/services/SynchronizeService.ts

View workflow job for this annotation

GitHub Actions / Quality Checks

There should be at least one empty line between import groups
import { getDeviceName } from 'react-native-device-info'

import type CozyClient from 'cozy-client'
Expand All @@ -9,6 +9,8 @@

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 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)
})
}
2 changes: 1 addition & 1 deletion src/app/domain/io.cozy.files/importantFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getErrorMessage } from '/libs/functions/getErrorMessage'

const log = Minilog('📁 Offline Files')

const IMPORTANT_FILES_DOWNLOAD_DELAY_IN_MS = 100
const IMPORTANT_FILES_DOWNLOAD_DELAY_IN_MS = 30 * 1000
const NB_OF_MONTH_BEFORE_EXPIRATION = 1

export const makeImportantFilesAvailableOfflineInBackground = (
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useAppBootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { useHomeStateContext } from '/screens/home/HomeStateProvider'

const log = Minilog('useAppBootstrap')

const MANAGE_ICON_CACHE_DELAY_IN_MS = 30 * 1000

export const useAppBootstrap = client => {
const [markName] = useState(() => rnperformance.mark('useAppBootstrap'))
const [initialRoute, setInitialRoute] = useState('fetching')
Expand Down Expand Up @@ -176,7 +178,8 @@ export const useAppBootstrap = client => {
return
}

client && manageIconCache(client)
client &&
setTimeout(() => manageIconCache(client), MANAGE_ICON_CACHE_DELAY_IN_MS)
client && setSentryTag(SentryCustomTags.Instance, client.stackClient?.uri)

const subscription = Linking.addEventListener('url', ({ url }) => {
Expand Down
9 changes: 7 additions & 2 deletions src/pouchdb/getLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -37,8 +40,10 @@ export const offlineDoctypes = [
export const getLinks = (): CozyLink[] => {
const pouchLinkOptions = {
doctypes: offlineDoctypes,
initialSync: true,
periodicSync: true,
initialSync: false,
periodicSync: false,
syncDebounceDelayInMs: REPLICATION_DEBOUNCE,
syncDebounceMaxDelayInMs: REPLICATION_DEBOUNCE_MAX_DELAY,
platform: platformReactNative,
ignoreWarmup: true,
doctypesReplicationOptions: Object.fromEntries(
Expand Down
Loading