-
-
Save monkey-codes/b42c0919c5228b1619c816a0bb169966 to your computer and use it in GitHub Desktop.
WebFlux - WebSocket redux middleware
This file contains hidden or 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
| class NullSocket { | |
| send(message){ | |
| console.log(`Warning: send called on NullSocket, dispatch a ${WEBSOCKET_CONNECT} first`); | |
| } | |
| } | |
| function factory({messageToActionAdapter}) { | |
| let socket = new NullSocket(); | |
| return ({dispatch}) => { | |
| return next => action => { | |
| switch (action.type) { | |
| case WEBSOCKET_CONNECT: | |
| socket = new WebSocket(action.payload.url); | |
| socket.onmessage = (msg) => { | |
| dispatch(messageToActionAdapter(msg) || { type:WEBSOCKET_MESSAGE, payload: msg.data}); | |
| } | |
| break; | |
| case WEBSOCKET_SEND: | |
| socket.send(JSON.stringify(action.payload)); | |
| break; | |
| } | |
| return next(action); | |
| } | |
| } | |
| } | |
| export default factory; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment