Skip to content

Commit

Permalink
fix(Realtime): Resubscribe with same parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kosssi committed May 7, 2019
1 parent 29fbcb9 commit f1aba47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/cozy-realtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ class CozyRealtime {
const subscribeList = Object.keys(this._events)
.map(key => {
if (!key.includes(INDEX_KEY_SEPARATOR)) return
const [, type, id] = key.split(INDEX_KEY_SEPARATOR)
if (this._events[key].length === 0) return
let [, type, id] = key.split(INDEX_KEY_SEPARATOR)
if (id === 'undefined') id = undefined
return { type, id }
})
.filter(Boolean)
Expand Down
18 changes: 18 additions & 0 deletions packages/cozy-realtime/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ describe('CozyRealtime', () => {
expect(handler.mock.calls.length).toBe(1)
})

it('should relaunch socket with same parameters', async () => {
const handler = jest.fn()

await realtime.subscribe('created', type, handler)
const spy = jest.spyOn(realtime._socket, 'subscribe')
await realtime.subscribe('updated', type, handler)
await realtime.subscribe('updated', type, id, handler)
await realtime.unsubscribe('created', type, handler)
await pause(10)

realtime._resubscribe()
expect(spy).toHaveBeenCalledTimes(4)
expect(spy.mock.calls[0][0]).toEqual(spy.mock.calls[2][0]) // type
expect(spy.mock.calls[0][1]).toEqual(spy.mock.calls[2][1]) // id
expect(spy.mock.calls[1][0]).toEqual(spy.mock.calls[3][0]) // type
expect(spy.mock.calls[1][1]).toEqual(spy.mock.calls[3][1]) // id
})

it('should launch only one connection when multiple subscribe is call', async () => {
const handler = jest.fn()
const isOpened = jest.fn()
Expand Down

0 comments on commit f1aba47

Please sign in to comment.