Skip to content

Commit

Permalink
feat: Speed up app display
Browse files Browse the repository at this point in the history
We used to init search at app initilization. However, we noticed that
this can be time consuming and slow down the app openening. Thus, we
prefer to make the user wait for an extra few seconds for the first
search, to init the search at this moment, rather than waiting at each
app opening.  Note we previously have added the index persistence
feature, in order to speed up this initialization.
  • Loading branch information
paultranvan committed Feb 11, 2025
1 parent 54fa178 commit 2cb620d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
7 changes: 0 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -136,12 +135,6 @@ const App = ({ setClient }) => {
useOfflineDebugUniversalLinks(client)
usePerformancesUniversalLinks(client)

useEffect(() => {
if (client) {
initSearchEngine(client)
}
}, [client])

const {
LauncherDialog,
canDisplayLauncher,
Expand Down
31 changes: 26 additions & 5 deletions src/app/domain/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,41 @@ import { getData, storeData } from '/libs/localStore/storage'
const log = Minilog('📱🗂️ Search')

let searchEngine: SearchEngine | undefined = undefined
let searchInitPromise: Promise<SearchEngine> | null = null

const storage: StorageInterface = {
storeData: storeData,
getData: getData
}

export const initSearchEngine = (client: CozyClient): void => {
export const initSearchEngine = (client: CozyClient): Promise<SearchEngine> => {
log.debug('Init SearchEngine')
searchEngine = new SearchEngine(client, storage, CozyClientPerformanceApi)

if (!searchInitPromise) {
// Use promise to handle parallel init
searchInitPromise = (async (): Promise<SearchEngine> => {
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<unknown> => {
log.debug('Search for', query)
if (!searchEngine) return undefined

if (!searchEngine) {
searchEngine = await initSearchEngine(client)
}
return searchEngine.search(query, options)
}
15 changes: 11 additions & 4 deletions src/libs/intents/localMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ export const backToHome = (): Promise<null> => {
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<unknown> => {
if (!client) {
throw new Error('search should not be called with undefined client')
}
return doSearch(client, query, options)
}

const isAvailable = (featureName: string): Promise<boolean> => {
Expand Down Expand Up @@ -227,7 +234,7 @@ interface CustomMethods {
storeSharedMemory: (key: string, value: SharedMemoryData) => Promise<void>
removeSharedMemory: (key: string) => Promise<void>
setColorScheme: (colorScheme: string | undefined) => Promise<void>
search: (query: string, options: SearchOptions) => unknown
search: (query: string, options: SearchOptions) => Promise<unknown>
}

type WithOptions<T> = {
Expand Down Expand Up @@ -420,7 +427,7 @@ export const localMethods = (
_options: PostMeMessageOptions,
query: string,
options: SearchOptions
) => search(query, options),
) => search(client, query, options),
...mergedMethods
}
}

0 comments on commit 2cb620d

Please sign in to comment.