Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import { reset } from 'redux-form';
  2. import { Presence } from 'phoenix'; // new line
  3.  
  4. // new function
  5. const syncPresentUsers = (dispatch, presences) => {
  6. const presentUsers = [];
  7. Presence.list(presences, (id, { metas: [first] }) => first.user)
  8. .map(user => presentUsers.push(user));
  9. dispatch({ type: 'ROOM_PRESENCE_UPDATE', presentUsers });
  10. };
  11.  
  12. export function connectToChannel(socket, roomId) {
  13. return (dispatch) => {
  14. if (!socket) { return false; }
  15. const channel = socket.channel(`rooms:${roomId}`);
  16. let presences = {}; // new line
  17.  
  18. // new function
  19. channel.on('presence_state', (state) => {
  20. presences = Presence.syncState(presences, state);
  21. syncPresentUsers(dispatch, presences);
  22. });
  23.  
  24. // new function
  25. channel.on('presence_diff', (diff) => {
  26. presences = Presence.syncDiff(presences, diff);
  27. syncPresentUsers(dispatch, presences);
  28. });
  29.  
  30. channel.on('message_created', (message) => {
  31. dispatch({ type: 'MESSAGE_CREATED', message });
  32. });
  33.  
  34. channel.join().receive('ok', (response) => {
  35. dispatch({ type: 'ROOM_CONNECTED_TO_CHANNEL', response, channel });
  36. });
  37.  
  38. return false;
  39. };
  40. }
  41.  
  42. // ...rest of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement