Last active
November 12, 2017 05:24
-
-
Save oviava/5de55279a4cb0c985dce to your computer and use it in GitHub Desktop.
handling socket events with redux
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
const types = { | |
ADD_TODO: 'ADD_TODO', | |
DELETE_TODO: 'DELETE_TODO', | |
COMPLETE_TODO: 'COMPLETE_TODO', | |
}; | |
const socketEvents = { | |
ADD_TODO_SUCCESS: 'ADD_TODO_SUCCESS', | |
ADD_TODO_FAIL: 'ADD_TODO_FAIL', | |
}; | |
const metaType = { | |
SOCKET: 'SOCKET', | |
}; | |
function addTodo(todo) { | |
return {type: types.ADD_TODO, payload: todo, meta: metaType.SOCKET }; | |
}; | |
function deleteTodo(id) { | |
return {type: types.DELETE_TODO, payload: id, meta: metaType.SOCKET }; | |
}; | |
function completeTodo(id) { | |
return {type: types.COMPLETE_TODO, payload: id, meta: metaType.SOCKET }; | |
}; | |
const socketMiddleware = store => next => action => { | |
if (action.meta === metaType.SOCKET) { | |
socket.emit(action.type, { payload: action.payload}) | |
} | |
return next(action); | |
} | |
socketEvents.map((event) => { | |
socket.on(event, (payload) => { | |
dispatch({type: event, payload: payload}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment