Skip to content

Commit

Permalink
feat: Handle offline queries
Browse files Browse the repository at this point in the history
When a client is started without network, e.g. from the mobile app, we
can rely on PouchDB to query the required docs and build the search
indexes.
We used to rely on replication events to make the indexing, but this is
not working of there is no network.
  • Loading branch information
paultranvan committed Jan 27, 2025
1 parent bae4fe2 commit 706ce6e
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,24 @@ export class SearchEngine {
if (!this.client) {
return
}
let startReplicationTime = 0,
endReplicationTime = 0
if (!this.isLocalSearch) {
// In case of non-local search, force the indexing for all doctypes
// For local search, this will be done automatically after initial replication
for (const doctype of SEARCHABLE_DOCTYPES) {
this.searchIndexes[doctype] = await this.indexDocsForSearch(
doctype as keyof typeof SEARCH_SCHEMA
)
}

// Create search indexes by querying docs, either locally or remotely
for (const doctype of SEARCHABLE_DOCTYPES) {
const startIndexingTime = performance.now()
this.searchIndexes[doctype] = await this.indexDocsForSearch(
doctype as keyof typeof SEARCH_SCHEMA
)
const endIndexingTime = performance.now()
log.debug(
`Indexing ${doctype} took ${(
endIndexingTime - startIndexingTime
).toFixed(2)}`
)
}

if (this.isLocalSearch) {
this.client.on('pouchlink:doctypesync:end', async (doctype: string) => {
if (isSearchedDoctype(doctype)) {
// Index doctype after initial replication
this.searchIndexes[doctype] = await this.indexDocsForSearch(
doctype as keyof typeof SEARCH_SCHEMA
)
}
})
this.client.on('pouchlink:sync:start', () => {
log.debug('Started pouch replication')
startReplicationTime = performance.now()
})
this.client.on('pouchlink:sync:end', () => {
log.debug('Ended pouch replication')
endReplicationTime = performance.now()
log.debug(
`Replication took ${(
endReplicationTime - startReplicationTime
).toFixed(2)}`
)
})
// Use replication events to have up-to-date search indexes, based on local data
this.indexOnReplicationEvents()
}

if (this.client.isLogged) {
Expand All @@ -113,6 +98,33 @@ export class SearchEngine {
}
}

indexOnReplicationEvents(): void {
let startReplicationTime = 0,
endReplicationTime = 0

this.client.on('pouchlink:doctypesync:end', async (doctype: string) => {
if (isSearchedDoctype(doctype)) {
// Index doctype after initial replication
this.searchIndexes[doctype] = await this.indexDocsForSearch(
doctype as keyof typeof SEARCH_SCHEMA
)
}
})
this.client.on('pouchlink:sync:start', () => {
log.debug('Started pouch replication')
startReplicationTime = performance.now()
})
this.client.on('pouchlink:sync:end', () => {
log.debug('Ended pouch replication')
endReplicationTime = performance.now()
log.debug(
`Replication took ${(endReplicationTime - startReplicationTime).toFixed(
2
)}`
)
})
}

afterLogin(): void {
// Ensure login is done before plugin register
this.client.registerPlugin(RealtimePlugin, {})
Expand Down

0 comments on commit 706ce6e

Please sign in to comment.