Skip to content

Commit

Permalink
feat: Move useRealtime from cozy-ui to cozy-realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Mar 10, 2021
1 parent 3763269 commit 229e8f5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cozy-realtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.12.2",
"description": "Realtime interactions with cozy-stack using Websocket",
"main": "dist/index.js",
"main": "dist/index.browser.js",
"author": "Cozy",
"license": "MIT",
"homepage": "https://docs.cozy.io/en/cozy-realtime/README/",
Expand Down
5 changes: 5 additions & 0 deletions packages/cozy-realtime/src/index.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CozyRealtime from './CozyRealtime'

export { default as useRealtime } from './useRealtime'
export { default as RealtimePlugin } from './RealtimePlugin'
export default CozyRealtime
48 changes: 48 additions & 0 deletions packages/cozy-realtime/src/useRealtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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?`
)
}
})
})
}

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(() => {
subscribeRealtime()

return unsubscribeRealtime
}, deps)
}

export default useRealtime
41 changes: 41 additions & 0 deletions packages/cozy-realtime/src/useRealtime.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import useRealtime from './useRealtime'
import { renderHook } from '@testing-library/react-hooks'

it('should subscribe to realtime events according to given specs', () => {
const onCreateOrUpdate = jest.fn()

const specs = {
'io.cozy.apps': {
created: onCreateOrUpdate
},
'io.cozy.contacts': {
updated: onCreateOrUpdate
}
}

const mockClient = {
plugins: {
realtime: {
subscribe: jest.fn(),
unsubscribe: jest.fn(),
unsubscribeAll: jest.fn()
}
}
}

renderHook(() => useRealtime(mockClient, specs, []))

expect(mockClient.plugins.realtime.subscribe).toHaveBeenNthCalledWith(
1,
'created',
'io.cozy.apps',
onCreateOrUpdate
)

expect(mockClient.plugins.realtime.subscribe).toHaveBeenNthCalledWith(
2,
'updated',
'io.cozy.contacts',
onCreateOrUpdate
)
})

0 comments on commit 229e8f5

Please sign in to comment.