Skip to content

Commit d0e7ea9

Browse files
author
doubleface
committed
feat: Allow to clean the pouches with dataproxy.cleanPouches flag
1 parent 05fa301 commit d0e7ea9

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
22

33
import { useClient } from 'cozy-client'
4+
import flag from 'cozy-flags'
45
import Minilog from 'cozy-minilog'
56

67
import { ParentWindowProvider } from '@/dataproxy/parent/ParentWindowProvider'
@@ -15,6 +16,12 @@ const App = (): JSX.Element => {
1516
const client = useClient()
1617
const { worker, workerState } = useSharedWorker()
1718

19+
useEffect(() => {
20+
if (flag('dataproxy.cleanPouch') === true) {
21+
worker.forceSyncPouch({ clean: true })
22+
}
23+
}, [])
24+
1825
return (
1926
<div className="content">
2027
<h1>Cozy DataProxy</h1>

src/dataproxy/common/DataProxyInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface DataProxyWorker {
1717
clientData: ClientData,
1818
options?: { sharedDriveIds: string[] }
1919
) => Promise<void>
20-
forceSyncPouch: () => void
20+
forceSyncPouch: (options?: { clean: boolean }) => Promise<void>
2121
requestLink: (
2222
definition: QueryDefinition | Mutation,
2323
options?: QueryOptions | MutationOptions

src/dataproxy/worker/worker.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,24 @@ const dataProxy: DataProxyWorker = {
188188
return queryRes
189189
},
190190

191-
forceSyncPouch: () => {
191+
forceSyncPouch: async (
192+
options: { clean: boolean } = { clean: false }
193+
): Promise<void> => {
192194
if (!client) {
193195
throw new Error(
194196
'Client is required to execute a forceSyncPouch, please initialize CozyClient'
195197
)
196198
}
197199
const pouchLink = getPouchLink(client)
198200
if (pouchLink) {
201+
if (options.clean) {
202+
await pouchLink.reset()
203+
await pouchLink.registerClient(client) // because reset also resets the client
204+
await pouchLink.onLogin()
205+
} else {
206+
await pouchLink.pouches.waitForCurrentReplications()
207+
}
208+
199209
pouchLink.startReplication()
200210
}
201211
},
@@ -221,7 +231,7 @@ const dataProxy: DataProxyWorker = {
221231
const replicationOptions = { strategy: 'fromRemote', driveId }
222232
const options = { shouldStartReplication: true }
223233
if (pouchLink.addDoctype) {
224-
pouchLink.addDoctype(doctype, replicationOptions, options)
234+
await pouchLink.addDoctype(doctype, replicationOptions, options)
225235
}
226236
}
227237

@@ -232,15 +242,15 @@ const dataProxy: DataProxyWorker = {
232242
searchEngine.addSharedDrive(driveId)
233243
},
234244

235-
removeSharedDrive: (driveId: string) => {
245+
removeSharedDrive: async (driveId: string) => {
236246
log.debug(`Worker: Removing shared drive ${driveId}`)
237247
if (!client) {
238248
throw new Error('Client is required to remove a shared drive')
239249
}
240250
const pouchLink = getPouchLink(client)
241251
if (pouchLink) {
242252
const doctype = `${SHARED_DRIVE_FILE_DOCTYPE}-${driveId}`
243-
pouchLink.removeDoctype(doctype)
253+
await pouchLink.removeDoctype(doctype)
244254
}
245255

246256
if (!searchEngine) {

0 commit comments

Comments
 (0)