Skip to content

Commit

Permalink
fix: Do not call useEffect conditionally
Browse files Browse the repository at this point in the history
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
ptbrowne committed Mar 10, 2021
1 parent aa54e68 commit 0984f53
Showing 1 changed file with 34 additions and 35 deletions.
69 changes: 34 additions & 35 deletions packages/cozy-realtime/src/useRealtime.js
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

0 comments on commit 0984f53

Please sign in to comment.