-
Notifications
You must be signed in to change notification settings - Fork 1
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: Trigger replication on appstate changes and realtime events #1278
Merged
Ldoppea
merged 5 commits into
feat/meta_offline
from
feat/trigger_replication_on_appstate_and_realtime
Jan 17, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f607738
feat: Add cozy-realtime package
Ldoppea face103
refactor: Move `triggerPouchReplication()` code into its own file
Ldoppea a4ef782
feat: Trigger PouchDB replication when apps is restored from background
Ldoppea 813a599
feat: Register CozyRealtime plugin into CozyClient
Ldoppea 77c1030
feat: Trigger PouchDB replication when realtime event is received
Ldoppea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/app/domain/offline/hooks/useOfflineReplicationOnRealtime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import CozyClient from 'cozy-client' | ||
import Minilog from 'cozy-minilog' | ||
import PouchLink from 'cozy-pouch-link' | ||
|
||
const log = Minilog('📶 Offline utils') | ||
|
||
export const triggerPouchReplication = (client?: CozyClient): void => { | ||
log.debug('Trigger PouchReplication (debounce)') | ||
const pouchLink = getPouchLink(client) | ||
pouchLink?.startReplicationWithDebounce() | ||
} | ||
|
||
export const getPouchLink = (client?: CozyClient): PouchLink | null => { | ||
if (!client) { | ||
return null | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return client.links.find(link => link instanceof PouchLink) || null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize we trigger a full replication on all doctypes for any realtime change, while we probably should only replicate for the concerned doctype, to save resources. But this is an improvement that can be made for later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we will have to implement something to handle that scenario. For now the replication method always replicate all pouches and no mechanism has been implemented to debounce replications separately.