Skip to content

Commit

Permalink
feat: Trigger PouchDB replication when realtime event is received
Browse files Browse the repository at this point in the history
  • Loading branch information
Ldoppea committed Jan 16, 2025
1 parent 4a9ed72 commit 11582fc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
} from '/screens/home/hooks/useLauncherContext'
import LauncherView from '/screens/konnectors/LauncherView'
import { makeImportantFilesAvailableOfflineInBackground } from '/app/domain/io.cozy.files/importantFiles'
import { useOfflineReplicationOnRealtime } from '/app/domain/offline/hooks/useOfflineReplicationOnRealtime'
import { useShareFiles } from '/app/domain/osReceive/services/shareFilesService'
import { ClouderyOffer } from '/app/view/IAP/ClouderyOffer'
import { useDimensions } from '/libs/dimensions'
Expand Down Expand Up @@ -130,6 +131,7 @@ const App = ({ setClient }) => {
useNotifications()
useGeolocationTracking()
useCozyEnvironmentOverride()
useOfflineReplicationOnRealtime()
useOfflineDebugUniversalLinks(client)
usePerformancesUniversalLinks(client)

Expand Down
54 changes: 54 additions & 0 deletions src/app/domain/offline/hooks/useOfflineReplicationOnRealtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect } from 'react'

import { useClient } from 'cozy-client'
import type { CozyClientDocument } from 'cozy-client/types/types'
import Minilog from 'cozy-minilog'

import { triggerPouchReplication } from '/app/domain/offline/utils'
import { offlineDoctypes } from '/pouchdb/getLinks'

const log = Minilog('📶 useOfflineReplicationOnRealtime')

export const useOfflineReplicationOnRealtime = (): void => {
const client = useClient()

useEffect(() => {
if (!client) return

// @ts-expect-error client.plugins is not typed
const realtime = client.plugins.realtime as CozyRealtime

const triggerReplication =
(verb: string) =>
(doc: CozyClientDocument): void => {
const docInfo = `${verb} ${doc._type ?? ''} ${doc._id ?? ''}`
log.debug(`Trigger replication from realtime event (${docInfo})`)
triggerPouchReplication(client)
}

const triggerReplicationCreated = triggerReplication('created')
const triggerReplicationUpdated = triggerReplication('updated')
const triggerReplicationDeleted = triggerReplication('deleted')

offlineDoctypes.forEach(doctype => {
realtime.subscribe('created', doctype, triggerReplicationCreated)
realtime.subscribe('updated', doctype, triggerReplicationUpdated)
realtime.subscribe('deleted', doctype, triggerReplicationDeleted)
})

return () => {
offlineDoctypes.forEach(doctype => {
realtime.unsubscribe('created', doctype, triggerReplicationCreated)
realtime.unsubscribe('updated', doctype, triggerReplicationUpdated)
realtime.unsubscribe('deleted', doctype, triggerReplicationDeleted)
})
}
})
}

interface CozyRealtime {
subscribe: (event: string, type: string, handler: Subscription) => void
unsubscribe: (event: string, type: string, handler: Subscription) => void
}

type Subscription = (doc: CozyClientDocument) => void

0 comments on commit 11582fc

Please sign in to comment.