Last active
October 22, 2016 15:13
-
-
Save bnhansn/7282180a673bc526d86b7bfc59c09693 to your computer and use it in GitHub Desktop.
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
import { reset } from 'redux-form'; | |
export function connectToChannel(socket, roomId) { | |
return (dispatch) => { | |
if (!socket) { return false; } | |
const channel = socket.channel(`rooms:${roomId}`); | |
// new function | |
channel.on('message_created', (message) => { | |
dispatch({ type: 'MESSAGE_CREATED', message }); | |
}); | |
channel.join().receive('ok', (response) => { | |
dispatch({ type: 'ROOM_CONNECTED_TO_CHANNEL', response, channel }); | |
}); | |
return false; | |
}; | |
} | |
export function leaveChannel(channel) { | |
return (dispatch) => { | |
if (channel) { | |
channel.leave(); | |
} | |
dispatch({ type: 'USER_LEFT_ROOM' }); | |
}; | |
} | |
// new function | |
export function createMessage(channel, data) { | |
return dispatch => new Promise((resolve, reject) => { | |
channel.push('new_message', data) | |
.receive('ok', () => resolve( | |
dispatch(reset('newMessage')) | |
)) | |
.receive('error', () => reject()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment