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

[PRD-525] [PRD-956] Rely on PouchDB for search #3

Merged
merged 5 commits into from
Oct 21, 2024
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
12 changes: 6 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ const config = [
'@typescript-eslint': tseslint.plugin
},
rules: {
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'warn',
'@typescript-eslint/no-unsafe-call': 'warn',
'@typescript-eslint/no-unsafe-member-access': 'warn',
'@typescript-eslint/no-unsafe-return': 'warn',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': [
'error',
{ ignoreRestSiblings: true }
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
"cozy-flags": "^4.0.0",
"cozy-logger": "^1.10.4",
"cozy-minilog": "^3.3.1",
"cozy-pouch-link": "^49.8.0",
"cozy-realtime": "^5.0.2",
"cozy-tsconfig": "^1.2.0",
"flexsearch": "^0.7.43",
"lodash": "^4.17.21",
"pouchdb-browser": "^9.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand Down
1 change: 1 addition & 0 deletions src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
}

export default class CozyClient {
plugins: any

Check warning on line 173 in src/@types/cozy-client.d.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unexpected any. Specify a different type
constructor(rawOptions?: ClientOptions)
getStackClient(): StackClient
getInstanceOptions(): InstanceOptions
Expand Down
2 changes: 1 addition & 1 deletion src/dataproxy/common/DataProxyInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { InstanceOptions } from 'cozy-client'
import type { ClientCapabilities } from 'cozy-client/types/types'

export type { SearchIndex } from '@/search/types'
export type { SearchIndexes } from '@/search/types'

export interface DataProxyWorker {
search: (query: string) => Promise<unknown>
Expand Down
110 changes: 110 additions & 0 deletions src/dataproxy/worker/platformWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import PouchDB from 'pouchdb-browser'

const dbName = 'sharedWorkerStorage'
let db: IDBDatabase | null = null

const openDB = (): Promise<IDBDatabase> => {
return new Promise((resolve, reject) => {
if (db) {
return resolve(db)
}

const request = indexedDB.open(dbName, 1)

request.onupgradeneeded = (event: IDBVersionChangeEvent): void => {
const database = (event.target as IDBOpenDBRequest).result
db = database
if (!db.objectStoreNames.contains('store')) {
db.createObjectStore('store', { keyPath: 'key' })
}
}

request.onsuccess = (event: Event): void => {
db = (event.target as IDBOpenDBRequest).result
resolve(db)
}

request.onerror = (event: Event): void => {
reject((event.target as IDBOpenDBRequest).error)
}
})
}

const storage = {
getItem: async (key: string): Promise<any> => {

Check warning on line 34 in src/dataproxy/worker/platformWorker.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unexpected any. Specify a different type
const db = await openDB()
return new Promise((resolve, reject) => {
const transaction = db.transaction('store', 'readonly')
const store = transaction.objectStore('store')
const request = store.get(key)

request.onsuccess = (): void => {
resolve(request.result ? request.result.value : null)

Check warning on line 42 in src/dataproxy/worker/platformWorker.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unsafe member access .value on an `any` value
}

request.onerror = (): void => {
reject(request.error)
}
})
},

setItem: async (key: string, value: any): Promise<void> => {

Check warning on line 51 in src/dataproxy/worker/platformWorker.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unexpected any. Specify a different type
const db = await openDB()
return new Promise((resolve, reject) => {
const transaction = db.transaction('store', 'readwrite')
const store = transaction.objectStore('store')
const request = store.put({ key, value })

Check warning on line 56 in src/dataproxy/worker/platformWorker.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unsafe assignment of an `any` value

request.onsuccess = (): void => {
resolve()
}

request.onerror = (): void => {
reject(request.error)
}
})
},

removeItem: async (key: string): Promise<void> => {
const db = await openDB()
return new Promise((resolve, reject) => {
const transaction = db.transaction('store', 'readwrite')
const store = transaction.objectStore('store')
const request = store.delete(key)

request.onsuccess = (): void => {
resolve()
}

request.onerror = (): void => {
reject(request.error)
}
})
}
}

const events = {
addEventListener: (
eventName: string,
handler: EventListenerOrEventListenerObject
): void => {
self.addEventListener(eventName, handler)
},
removeEventListener: (
eventName: string,
handler: EventListenerOrEventListenerObject
): void => {
self.removeEventListener(eventName, handler)
}
}

const isOnline = async (): Promise<boolean> => {
return self.navigator.onLine
}

export const platformWorker = {
storage,
events,
pouchAdapter: PouchDB,

Check warning on line 108 in src/dataproxy/worker/platformWorker.ts

View workflow job for this annotation

GitHub Actions / Build and publish

Unsafe assignment of an `any` value
isOnline
}
71 changes: 47 additions & 24 deletions src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,51 @@ import * as Comlink from 'comlink'

import CozyClient from 'cozy-client'
import Minilog from 'cozy-minilog'
import PouchLink from 'cozy-pouch-link'

import {
ClientData,
DataProxyWorker,
DataProxyWorkerPartialState,
SearchIndex
DataProxyWorkerPartialState
} from '@/dataproxy/common/DataProxyInterface'
import { platformWorker } from '@/dataproxy/worker/platformWorker'
import schema from '@/doctypes'
import { initIndexes } from '@/search/initIndexes'
import { search } from '@/search/search'
import SearchEngine from '@/search/SearchEngine'
import { FILES_DOCTYPE, CONTACTS_DOCTYPE, APPS_DOCTYPE } from '@/search/consts'

const log = Minilog('👷‍♂️ [shared-worker]')
Minilog.enable()

let client: CozyClient | undefined = undefined
let searchIndexes: SearchIndex[] | undefined = undefined
let searchEngine: SearchEngine

const broadcastChannel = new BroadcastChannel('DATA_PROXY_BROADCAST_CHANANEL')

const dataProxy: DataProxyWorker = {
// FIXME: change setClient name
setClient: async (clientData: ClientData) => {
log.debug('Received data for setting client')
if (client) return
updateState()

const pouchLinkOptions = {
doctypes: [FILES_DOCTYPE, CONTACTS_DOCTYPE, APPS_DOCTYPE],
initialSync: true,
periodicSync: false,
platform: { ...platformWorker },
doctypesReplicationOptions: {
[FILES_DOCTYPE]: {
strategy: 'fromRemote'
},
[CONTACTS_DOCTYPE]: {
strategy: 'fromRemote'
},
[APPS_DOCTYPE]: {
strategy: 'fromRemote'
}
}
}

client = new CozyClient({
uri: clientData.uri,
token: clientData.token,
Expand All @@ -34,43 +55,45 @@ const dataProxy: DataProxyWorker = {
version: '1'
},
schema,
store: true
store: true,
links: [new PouchLink(pouchLinkOptions)]
})
client.instanceOptions = clientData.instanceOptions
client.capabilities = clientData.capabilities
if (!searchIndexes) {
const indexes = await initIndexes(client)
searchIndexes = indexes
updateState()
}

searchEngine = new SearchEngine(client)

updateState()
},

search: async (query: string) => {
log.debug('Received data for search')
if (!client) {
throw new Error(
'Client is required to execute a seach, please initialize CozyClient'
'Client is required to execute a search, please initialize CozyClient'
)
}

if (!searchIndexes) {
return []
if (!searchEngine) {
throw new Error('SearchEngine is not initialized')
}

return search(query, searchIndexes, client)
return searchEngine.search(query)
}
}

const updateState = (): void => {
const state = {} as DataProxyWorkerPartialState

if (client && searchIndexes) {
if (client && searchEngine && searchEngine.searchIndexes) {
state.status = 'Ready'
state.indexLength = searchIndexes.map(searchIndex => ({
doctype: searchIndex.doctype,
// @ts-expect-error index.store is not TS typed
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
count: Object.keys(searchIndex.index.store).length
}))
state.indexLength = Object.keys(searchEngine.searchIndexes).map(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: here you can use Object.entries().map(([key, value]) => {...} instead of Object.keys

    state.indexLength = Object.entries(searchEngine.searchIndexes).map(
      ([key, value]) => ({
        doctype: key,
        count: Object.keys(value.index.store).length
      })

Copy link
Contributor Author

@paultranvan paultranvan Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, I made the change in 811b02c

(indexKey: string) => ({
doctype: indexKey,
// @ts-expect-error index.store is not TS typed
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
count: Object.keys(searchEngine.searchIndexes[indexKey].index.store)
.length
})
)
broadcastChannel.postMessage(state)
return
}
Expand Down
Loading