summaryrefslogtreecommitdiff
path: root/frontend/src/containers/App/saga.js
blob: 0c212142281ab3f53c6e743a665cde6c93ad652f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { put, take } from 'redux-saga/effects'
import { eventChannel } from 'redux-saga'

function createSocketChannel(socket) {
  return eventChannel(emit => {
    const errorHandler = event => emit(JSON.parse(event.body))

    const userErrors = socket.subscribe('/user/queue/errors', errorHandler)

    const unsubscribe = () => {
      userErrors.unsubscribe()
    }

    return unsubscribe
  })
}

export function* watchOnErrors(socketConnection) {
  const { socket } = socketConnection
  const socketChannel = createSocketChannel(socket)

  while (true) {
    const payload = yield take(socketChannel)
    yield put({ type: 'USER_ERROR', payload })
  }
}

export default watchOnErrors
bgstack15