-
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.
feat: Move useRealtime from cozy-ui to cozy-realtime
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 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
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,5 @@ | ||
import CozyRealtime from './CozyRealtime' | ||
|
||
export { default as useRealtime } from './useRealtime' | ||
export { default as RealtimePlugin } from './RealtimePlugin' | ||
export default CozyRealtime |
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,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 |
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,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 | ||
) | ||
}) |