summaryrefslogtreecommitdiff
path: root/src/main/js/src/containers/App/saga.js
blob: 4ccf601965bdcd0bc78ec534ade469817f81b9c8 (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
29
30
31
32
33
34
35
36
37
38
import { call, put, take } from 'redux-saga/effects'
import { eventChannel } from 'redux-saga'
import createWebSocketConnection from "../../utils/createWebSocketConnection";

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

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

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

    return unsubscribe
  })
}

export function* watchOnErrors() {
  let socketChannel
  try {
    const { socket } = yield call(createWebSocketConnection)
    socketChannel = createSocketChannel(socket)
  } catch (error) {
    console.error('Error connecting to socket', error)
  }

  if (!socketChannel) {
    return
  }

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

export default watchOnErrors
bgstack15