diff --git a/src/App.js b/src/App.js index 050b3cdd7..238325336 100644 --- a/src/App.js +++ b/src/App.js @@ -73,7 +73,6 @@ import LauncherView from '/screens/konnectors/LauncherView' import { makeImportantFilesAvailableOfflineInBackground } from '/app/domain/io.cozy.files/importantFiles' import { useOfflineReplicationOnRealtime } from '/app/domain/offline/hooks/useOfflineReplicationOnRealtime' import { useShareFiles } from '/app/domain/osReceive/services/shareFilesService' -import { initSearchEngine } from '/app/domain/search/search' import { ClouderyOffer } from '/app/view/IAP/ClouderyOffer' import { useDimensions } from '/libs/dimensions' import { configureFileLogger } from '/app/domain/logger/fileLogger' @@ -136,12 +135,6 @@ const App = ({ setClient }) => { useOfflineDebugUniversalLinks(client) usePerformancesUniversalLinks(client) - useEffect(() => { - if (client) { - initSearchEngine(client) - } - }, [client]) - const { LauncherDialog, canDisplayLauncher, diff --git a/src/app/domain/search/search.ts b/src/app/domain/search/search.ts index 60061bab8..788ce6e39 100644 --- a/src/app/domain/search/search.ts +++ b/src/app/domain/search/search.ts @@ -12,20 +12,41 @@ import { getData, storeData } from '/libs/localStore/storage' const log = Minilog('📱🗂️ Search') let searchEngine: SearchEngine | undefined = undefined +let searchInitPromise: Promise | null = null const storage: StorageInterface = { storeData: storeData, getData: getData } -export const initSearchEngine = (client: CozyClient): void => { +export const initSearchEngine = (client: CozyClient): Promise => { log.debug('Init SearchEngine') - searchEngine = new SearchEngine(client, storage, CozyClientPerformanceApi) + + if (!searchInitPromise) { + // Use promise to handle parallel init + searchInitPromise = (async (): Promise => { + searchEngine = new SearchEngine( + client, + storage, + CozyClientPerformanceApi, + { shouldInit: false } // Do the init manually to ensure all the indexes are ready + ) + await searchEngine.init() // Once the init is done, we can safely search + searchInitPromise = null + return searchEngine + })() + } + return searchInitPromise } -export const search = (query: string, options: SearchOptions): unknown => { +export const search = async ( + client: CozyClient, + query: string, + options: SearchOptions +): Promise => { log.debug('Search for', query) - if (!searchEngine) return undefined - + if (!searchEngine) { + searchEngine = await initSearchEngine(client) + } return searchEngine.search(query, options) } diff --git a/src/libs/intents/localMethods.ts b/src/libs/intents/localMethods.ts index caa9f2f4f..c79f73475 100644 --- a/src/libs/intents/localMethods.ts +++ b/src/libs/intents/localMethods.ts @@ -97,8 +97,15 @@ export const backToHome = (): Promise => { return Promise.resolve(null) } -const search = (query: string, options: SearchOptions): unknown => { - return doSearch(query, options) +const search = async ( + client: CozyClient | undefined, + query: string, + options: SearchOptions +): Promise => { + if (!client) { + throw new Error('search should not be called with undefined client') + } + return doSearch(client, query, options) } const isAvailable = (featureName: string): Promise => { @@ -227,7 +234,7 @@ interface CustomMethods { storeSharedMemory: (key: string, value: SharedMemoryData) => Promise removeSharedMemory: (key: string) => Promise setColorScheme: (colorScheme: string | undefined) => Promise - search: (query: string, options: SearchOptions) => unknown + search: (query: string, options: SearchOptions) => Promise } type WithOptions = { @@ -420,7 +427,7 @@ export const localMethods = ( _options: PostMeMessageOptions, query: string, options: SearchOptions - ) => search(query, options), + ) => search(client, query, options), ...mergedMethods } }