Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { reset } from 'redux-form';
- import { Presence } from 'phoenix'; // new line
- // new function
- const syncPresentUsers = (dispatch, presences) => {
- const presentUsers = [];
- Presence.list(presences, (id, { metas: [first] }) => first.user)
- .map(user => presentUsers.push(user));
- dispatch({ type: 'ROOM_PRESENCE_UPDATE', presentUsers });
- };
- export function connectToChannel(socket, roomId) {
- return (dispatch) => {
- if (!socket) { return false; }
- const channel = socket.channel(`rooms:${roomId}`);
- let presences = {}; // new line
- // new function
- channel.on('presence_state', (state) => {
- presences = Presence.syncState(presences, state);
- syncPresentUsers(dispatch, presences);
- });
- // new function
- channel.on('presence_diff', (diff) => {
- presences = Presence.syncDiff(presences, diff);
- syncPresentUsers(dispatch, presences);
- });
- 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;
- };
- }
- // ...rest of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement