-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Do not call useEffect conditionally
The check on client.plugins would return early if the client did not have the realtime plugin; thus, useEffect was called conditionally. Now the check for plugins is done inside the useEffect.
- Loading branch information
Showing
1 changed file
with
34 additions
and
35 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
import { useEffect } from 'react' | ||
|
||
const useRealtime = (client, specs, deps) => { | ||
if (!client.plugins || !client.plugins.realtime) { | ||
console.error( | ||
'[useRealtime] The provided CozyClient instance does not have a RealtimePlugin registered' | ||
) | ||
return | ||
} | ||
|
||
const subscribeRealtime = () => { | ||
Object.entries(specs).forEach(([doctype, events]) => { | ||
Object.entries(events).forEach(async ([event, callback]) => { | ||
try { | ||
await client.plugins.realtime.subscribe(event, doctype, callback) | ||
} catch (err) { | ||
console.error(err) | ||
console.error( | ||
`[useRealtime] Impossible to subscribe to ${event} event on ${doctype}. Does your app have the required permissions on this doctype?` | ||
) | ||
} | ||
useEffect(() => { | ||
const subscribeRealtime = () => { | ||
Object.entries(specs).forEach(([doctype, events]) => { | ||
Object.entries(events).forEach(async ([event, callback]) => { | ||
try { | ||
await client.plugins.realtime.subscribe(event, doctype, callback) | ||
} catch (err) { | ||
console.error(err) | ||
console.error( | ||
`[useRealtime] Impossible to subscribe to ${event} event on ${doctype}. Does your app have the required permissions on this doctype?` | ||
) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
const unsubscribeRealtime = () => { | ||
Object.entries(specs).forEach(([doctype, events]) => { | ||
Object.entries(events).forEach(async ([event, callback]) => { | ||
try { | ||
await client.plugins.realtime.unsubscribe(event, doctype, callback) | ||
} catch (err) { | ||
console.error(err) | ||
console.error( | ||
`[useRealtime] Impossible to unsubscribe from ${event} event on ${doctype}. Does your app have the required permissions on this doctype?` | ||
) | ||
} | ||
const unsubscribeRealtime = () => { | ||
Object.entries(specs).forEach(([doctype, events]) => { | ||
Object.entries(events).forEach(async ([event, callback]) => { | ||
try { | ||
await client.plugins.realtime.unsubscribe(event, doctype, callback) | ||
} catch (err) { | ||
console.error(err) | ||
console.error( | ||
`[useRealtime] Impossible to unsubscribe from ${event} event on ${doctype}. Does your app have the required permissions on this doctype?` | ||
) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (!client.plugins || !client.plugins.realtime) { | ||
console.error( | ||
'[useRealtime] The provided CozyClient instance does not have a RealtimePlugin registered, useRealtime will not work' | ||
) | ||
return | ||
} | ||
subscribeRealtime() | ||
|
||
return unsubscribeRealtime | ||
}, deps) | ||
}, deps) // eslint-disable-line react-hooks/exhaustive-deps | ||
} | ||
|
||
export default useRealtime |