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

feat: Move SearchEngine to cozy-dataproxy library #10

Merged
merged 6 commits into from
Nov 6, 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
1 change: 1 addition & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import '@testing-library/jest-dom';
import 'cross-fetch/polyfill';
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default {
'\\.(css|less|scss|sass)$': '<rootDir>/tests/__mocks__/styleMock.js',
"^@/(.*)$": "<rootDir>/src/$1",
"^manifest.webapp$": ["<rootDir>/manifest.webapp"],
// Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
"uuid": require.resolve('uuid'),
},
resetMocks: true
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
"dependencies": {
"comlink": "^4.4.1",
"cozy-app-publish": "^0.34.0",
"cozy-client": "^50.1.0",
"cozy-client": "^50.3.1",
"cozy-dataproxy-lib": "^1.1.1",
"cozy-device-helper": "^3.1.0",
"cozy-flags": "^4.0.0",
"cozy-logger": "^1.10.4",
"cozy-minilog": "^3.3.1",
"cozy-pouch-link": "^50.1.0",
"cozy-pouch-link": "^50.3.1",
"cozy-realtime": "^5.0.2",
"cozy-tsconfig": "^1.2.0",
"flexsearch": "^0.7.43",
Expand All @@ -43,6 +44,7 @@
"@types/sharedworker": "^0.0.126",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"cross-fetch": "^4.0.0",
"eslint": "8.57.1",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
Expand Down
9 changes: 8 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Minilog.enable()

const App = (): JSX.Element => {
const client = useClient()
const { workerState } = useSharedWorker()
const { worker, workerState } = useSharedWorker()

return (
<div className="content">
Expand All @@ -28,6 +28,13 @@ const App = (): JSX.Element => {
</p>
)
})}
<button
onClick={() => {
worker.forceSyncPouch()
}}
>
Force sync
</button>
</div>
)
}
Expand Down
8 changes: 8 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const REPLICATION_DEBOUNCE = 30 * 1000 // 30s
export const REPLICATION_DEBOUNCE_MAX_DELAY = 600 * 1000 // 10min

export const FILES_DOCTYPE = 'io.cozy.files'
export const CONTACTS_DOCTYPE = 'io.cozy.contacts'
export const APPS_DOCTYPE = 'io.cozy.apps'

export const LOCALSTORAGE_KEY_DELETING_DATA = 'deletingLocalData'
3 changes: 1 addition & 2 deletions src/dataproxy/common/DataProxyInterface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { InstanceOptions } from 'cozy-client'
import type { ClientCapabilities } from 'cozy-client/types/types'

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

export interface DataProxyWorker {
search: (query: string) => unknown
setup: (clientData: ClientData) => Promise<void>
forceSyncPouch: () => void
}

export interface DataProxyWorkerPartialState {
Expand Down
25 changes: 23 additions & 2 deletions src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import * as Comlink from 'comlink'

import CozyClient from 'cozy-client'
import { SearchEngine } from 'cozy-dataproxy-lib'
import Minilog from 'cozy-minilog'
import PouchLink from 'cozy-pouch-link'

import {
FILES_DOCTYPE,
CONTACTS_DOCTYPE,
APPS_DOCTYPE,
REPLICATION_DEBOUNCE,
REPLICATION_DEBOUNCE_MAX_DELAY
} from '@/consts'
import {
ClientData,
DataProxyWorker,
DataProxyWorkerPartialState
} from '@/dataproxy/common/DataProxyInterface'
import { platformWorker } from '@/dataproxy/worker/platformWorker'
import schema from '@/doctypes'
import SearchEngine from '@/search/SearchEngine'
import { FILES_DOCTYPE, CONTACTS_DOCTYPE, APPS_DOCTYPE } from '@/search/consts'
import { getPouchLink } from '@/helpers/client'

const log = Minilog('👷‍♂️ [shared-worker]')
Minilog.enable()
Expand All @@ -32,6 +39,8 @@ const dataProxy: DataProxyWorker = {
doctypes: [FILES_DOCTYPE, CONTACTS_DOCTYPE, APPS_DOCTYPE],
initialSync: true,
periodicSync: false,
syncDebounceDelayInMs: REPLICATION_DEBOUNCE,
syncDebounceMaxDelayInMs: REPLICATION_DEBOUNCE_MAX_DELAY,
Copy link
Contributor

Choose a reason for hiding this comment

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

cozy/cozy-client#1553 must be merged first and new version included in this commit

platform: { ...platformWorker },
doctypesReplicationOptions: {
[FILES_DOCTYPE]: {
Expand Down Expand Up @@ -80,6 +89,18 @@ const dataProxy: DataProxyWorker = {
const endSearchTime = performance.now()
log.debug(`Search took ${endSearchTime - startSearchTime} ms`)
return results
},

forceSyncPouch: () => {
if (!client) {
throw new Error(
'Client is required to execute a forceSyncPouch, please initialize CozyClient'
)
}
const pouchLink = getPouchLink(client)
if (pouchLink) {
pouchLink.startReplication()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/dataproxy/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Minilog from 'cozy-minilog'

import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/search/consts'
import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/consts'
const log = Minilog('👷‍♂️ [Worker utils]')

const deleteDatabases = async (): Promise<void> => {
Expand Down
41 changes: 41 additions & 0 deletions src/helpers/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import CozyClient from 'cozy-client'
import PouchLink from 'cozy-pouch-link'
import { LinkPlatform } from 'cozy-pouch-link/types/types'

import { getPouchLink } from '@/helpers/client'

test('should retrieve PouchLink from client', () => {
const pouchLinkOptions = {
doctypes: ['io.cozy.files'],
initialSync: true,
periodicSync: false,
platform: {} as LinkPlatform,
doctypesReplicationOptions: {
'io.cozy.files': {
strategy: 'fromRemote'
}
}
}
const pouchLink = new PouchLink(pouchLinkOptions)
const client = new CozyClient({
links: [pouchLink]
})

const result = getPouchLink(client)

expect(result).toBe(pouchLink)
})

test('should return null if no client', () => {
const result = getPouchLink(undefined)

expect(result).toBe(null)
})

test('should return null if no PouchLink found', () => {
const client = new CozyClient()

const result = getPouchLink(client)

expect(result).toBe(null)
})
2 changes: 1 addition & 1 deletion src/search/helpers/client.ts → src/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CozyClient from 'cozy-client'
import PouchLink from 'cozy-pouch-link'

export const getPouchLink = (client: CozyClient): PouchLink | null => {
export const getPouchLink = (client?: CozyClient): PouchLink | null => {
if (!client) {
return null
}
Expand Down
151 changes: 0 additions & 151 deletions src/search/SearchEngine.spec.js

This file was deleted.

Loading