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

[FRONT-2375] feat: Allow init to be called independently #2730

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
32 changes: 20 additions & 12 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,30 @@ interface FlexSearchResultWithDoctype
doctype: SearchedDoctype
}

interface EngineOptions {
shouldInit?: boolean
}

export class SearchEngine {
client: CozyClient
searchIndexes: SearchIndexes
debouncedReplication: () => void
isLocalSearch: boolean
storage: StorageInterface
performanceApi: PerformanceAPI
engineOptions: EngineOptions

constructor(
client: CozyClient,
storage: StorageInterface,
performanceApi?: PerformanceAPI
performanceApi?: PerformanceAPI,
engineOptions: EngineOptions = {}
) {
this.client = client
this.searchIndexes = {} as SearchIndexes
this.storage = storage
this.performanceApi = performanceApi ?? defaultPerformanceApi
this.engineOptions = { shouldInit: true, ...engineOptions }

this.isLocalSearch = !!getPouchLink(this.client)
log.info('Use local data on trusted device: ', this.isLocalSearch)
Expand All @@ -78,12 +85,14 @@ export class SearchEngine {
}
}

if (this.client.isLogged) {
this.afterLogin()
} else {
this.client.on('login', () => {
this.afterLogin()
})
if (this.engineOptions.shouldInit) {
if (this.client.isLogged) {
void this.init()
} else {
this.client.on('login', () => {
void this.init()
})
}
}
}

Expand Down Expand Up @@ -161,10 +170,7 @@ export class SearchEngine {
})
}

afterLogin(): void {
// The document indexing should be performed once everything is setup
void this.indexDocuments()

async init(): Promise<void> {
// Ensure login is done before plugin register
if (!this.client.plugins[RealtimePlugin.pluginName]) {
this.client.registerPlugin(RealtimePlugin, {})
Expand All @@ -176,6 +182,9 @@ export class SearchEngine {
this.subscribeDoctype(this.client, FILES_DOCTYPE)
this.subscribeDoctype(this.client, CONTACTS_DOCTYPE)
this.subscribeDoctype(this.client, APPS_DOCTYPE)

// The document indexing should be performed once everything is setup
await this.indexDocuments()
}

subscribeDoctype(client: CozyClient, doctype: string): void {
Expand Down Expand Up @@ -370,7 +379,6 @@ export class SearchEngine {
markName: markeNameIndex,
category: 'Search'
})

return output
}

Expand Down
Loading