Skip to content

Commit

Permalink
fix: Prevent registering RealtimePlugin if it is already registered
Browse files Browse the repository at this point in the history
The flagship app already register the RealtimePlugin so the
SearchEngine would try to register it a second time

We assume the consuming app should be responsible to do the
registration so the solution would be to remove it from the
SearchEngine initialization

But in order to ensure backward compatibility, we chose to keep the
registration here but protected behind a check so the SearchEngine
would register it only if not already registered
  • Loading branch information
Ldoppea committed Jan 29, 2025
1 parent 74d2449 commit 24165f3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ declare module 'cozy-client' {
}

export default class CozyClient {
plugins: unknown
// eslint-disable-next-line @typescript-eslint/no-explicit-any
plugins: Record<string, any>
constructor(rawOptions?: ClientOptions)
getStackClient(): StackClient
getInstanceOptions(): InstanceOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
declare module 'cozy-realtime' {
export const RealtimePlugin = (): null => null
RealtimePlugin.pluginName = 'realtime'
}
5 changes: 3 additions & 2 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export class SearchEngine {
void this.indexDocuments()

// Ensure login is done before plugin register
this.client.registerPlugin(RealtimePlugin, {})
if (!this.client.plugins[RealtimePlugin.pluginName]) {
this.client.registerPlugin(RealtimePlugin, {})
}

// Realtime subscription
this.handleUpdatedOrCreatedDoc = this.handleUpdatedOrCreatedDoc.bind(this)
Expand All @@ -139,7 +141,6 @@ export class SearchEngine {

subscribeDoctype(client: CozyClient, doctype: string): void {
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/unbound-method */
// @ts-expect-error Client's plugins are not typed
const realtime = client.plugins.realtime
realtime.subscribe('created', doctype, this.handleUpdatedOrCreatedDoc)
realtime.subscribe('updated', doctype, this.handleUpdatedOrCreatedDoc)
Expand Down

0 comments on commit 24165f3

Please sign in to comment.