From 43a725c662b51f278cd093cefc7d26fe5971f07c Mon Sep 17 00:00:00 2001 From: khlieng Date: Thu, 22 Jan 2015 00:14:28 +0100 Subject: [PATCH] Cleaned up action naming and handling --- client/src/js/actions/channel.js | 41 ++++++++------ client/src/js/actions/message.js | 46 ++++++++++++++-- client/src/js/actions/server.js | 14 +++-- client/src/js/app.js | 65 +++-------------------- client/src/js/components/MessageInput.jsx | 10 +--- client/src/js/socket.js | 58 ++++++++------------ client/src/js/stores/channel.js | 20 +++---- client/src/js/stores/selectedTab.js | 6 +-- client/src/js/stores/server.js | 8 ++- 9 files changed, 129 insertions(+), 139 deletions(-) diff --git a/client/src/js/actions/channel.js b/client/src/js/actions/channel.js index 50e726f5..e7df3738 100644 --- a/client/src/js/actions/channel.js +++ b/client/src/js/actions/channel.js @@ -1,47 +1,54 @@ var Reflux = require('reflux'); +var _ = require('lodash'); -var sock = require('../socket.js')('/ws'); +var socket = require('../socket.js'); var channelActions = Reflux.createActions([ 'join', - 'joined', 'part', - 'parted', - 'quit', + 'addUser', + 'removeUser', + 'removeUserAll', 'setUsers', 'setTopic', 'load' ]); -channelActions.join.preEmit = function(data) { - sock.send('join', data); +channelActions.join.preEmit = function(channels, server) { + socket.send('join', { + server: server, + channels: channels + }); }; -channelActions.part.preEmit = function(data) { - sock.send('part', data); +channelActions.part.preEmit = function(channels, server) { + socket.send('part', { + server: server, + channels: channels + }); }; -sock.on('join', function(data) { - channelActions.joined(data.user, data.server, data.channels[0]); +socket.on('join', function(data) { + channelActions.addUser(data.user, data.server, data.channels[0]); }); -sock.on('part', function(data) { - channelActions.parted(data.user, data.server, data.channels[0]); +socket.on('part', function(data) { + channelActions.removeUser(data.user, data.server, data.channels[0]); }); -sock.on('quit', function(data) { - channelActions.quit(data.user, data.server); +socket.on('quit', function(data) { + channelActions.removeUserAll(data.user, data.server); }); -sock.on('users', function(data) { +socket.on('users', function(data) { channelActions.setUsers(data.users, data.server, data.channel); }); -sock.on('topic', function(data) { +socket.on('topic', function(data) { channelActions.setTopic(data.topic, data.server, data.channel); }); -sock.on('channels', function(data) { +socket.on('channels', function(data) { channelActions.load(data); }); diff --git a/client/src/js/actions/message.js b/client/src/js/actions/message.js index 0512919b..2db580a0 100644 --- a/client/src/js/actions/message.js +++ b/client/src/js/actions/message.js @@ -1,19 +1,57 @@ var Reflux = require('reflux'); -var sock = require('../socket.js')('/ws'); +var socket = require('../socket.js'); var messageActions = Reflux.createActions([ 'send', - 'add', - 'selectTab' + 'add' ]); messageActions.send.preEmit = function(message, to, server) { - sock.send('chat', { + socket.send('chat', { server: server, to: to, message: message }); }; +socket.on('message', function(data) { + messageActions.add(data); +}); + +socket.on('pm', function(data) { + messageActions.add(data); +}); + +socket.on('join', function(data) { + messageActions.add({ + server: data.server, + from: '', + to: data.channels[0], + message: data.user + ' joined the channel', + type: 'info' + }); +}); + +socket.on('part', function(data) { + messageActions.add({ + server: data.server, + from: '', + to: data.channels[0], + message: data.user + ' left the channel', + type: 'info' + }); +}); + +socket.on('motd', function(data) { + _.each(data.content.split('\n'), function(line) { + messageActions.add({ + server: data.server, + from: '', + to: data.server, + message: line + }); + }); +}); + module.exports = messageActions; \ No newline at end of file diff --git a/client/src/js/actions/server.js b/client/src/js/actions/server.js index 04e50bd6..9cb2b281 100644 --- a/client/src/js/actions/server.js +++ b/client/src/js/actions/server.js @@ -1,14 +1,22 @@ var Reflux = require('reflux'); -var sock = require('../socket.js')('/ws'); +var socket = require('../socket.js')('/ws'); var serverActions = Reflux.createActions([ 'connect', 'disconnect' ]); -serverActions.connect.preEmit = function(data) { - sock.send('connect', data); +serverActions.connect.preEmit = function(server, nick, username) { + socket.send('connect', { + server: server, + nick: nick, + username: username + }); +}; + +serverActions.disconnect.preEmit = function(server) { + socket.send('quit', { server: server }); }; module.exports = serverActions; \ No newline at end of file diff --git a/client/src/js/app.js b/client/src/js/app.js index bd9d7def..959d3880 100644 --- a/client/src/js/app.js +++ b/client/src/js/app.js @@ -1,70 +1,21 @@ var React = require('react'); -var _ = require('lodash'); -var sock = require('./socket')('/ws'); +var socket = require('./socket'); var util = require('./util'); var App = require('./components/App.jsx'); -var messageActions = require('./actions/message.js'); var tabActions = require('./actions/tab.js'); var serverActions = require('./actions/server.js'); var channelActions = require('./actions/channel.js'); -React.render(, document.body); - var uuid = localStorage.uuid || (localStorage.uuid = util.UUID()); +var nick = 'test' + Math.floor(Math.random() * 99999); -tabActions.select('irc.freenode.net'); +socket.on('connect', function() { + socket.send('uuid', uuid); -sock.on('connect', function() { - sock.send('uuid', uuid); - - serverActions.connect({ - server: 'irc.freenode.net', - nick: 'test' + Math.floor(Math.random() * 99999), - username: 'user' - }); - - channelActions.join({ - server: 'irc.freenode.net', - channels: [ '#stuff' ] - }); + serverActions.connect('irc.freenode.net', nick, 'username'); + channelActions.join(['#stuff'], 'irc.freenode.net'); + tabActions.select('irc.freenode.net'); }); -channelActions.joined.listen(function(user, server, channel) { - messageActions.add({ - server: server, - from: '', - to: channel, - message: user + ' joined the channel', - type: 'info' - }); -}); - -channelActions.parted.listen(function(user, server, channel) { - messageActions.add({ - server: server, - from: '', - to: channel, - message: user + ' left the channel', - type: 'info' - }); -}); - -sock.on('message', function(data) { - messageActions.add(data); -}); - -sock.on('pm', function(data) { - messageActions.add(data); -}); - -sock.on('motd', function(data) { - _.each(data.content.split('\n'), function(line) { - messageActions.add({ - server: data.server, - from: '', - to: data.server, - message: line - }); - }); -}); \ No newline at end of file +React.render(, document.body); \ No newline at end of file diff --git a/client/src/js/components/MessageInput.jsx b/client/src/js/components/MessageInput.jsx index d9d65bbc..721945b0 100644 --- a/client/src/js/components/MessageInput.jsx +++ b/client/src/js/components/MessageInput.jsx @@ -11,19 +11,13 @@ function dispatchCommand(cmd, channel, server) { switch (params[0].toLowerCase()) { case 'join': if (params[1]) { - channelActions.join({ - server: server, - channels: [params[1]] - }); + channelActions.join(params[1], server); } break; case 'part': if (channel) { - channelActions.part({ - server: server, - channels: [channel] - }); + channelActions.part([channel], server); } break; } diff --git a/client/src/js/socket.js b/client/src/js/socket.js index 60c1381a..ee5b0152 100644 --- a/client/src/js/socket.js +++ b/client/src/js/socket.js @@ -2,40 +2,28 @@ var EventEmitter = require('events').EventEmitter; var _ = require('lodash'); -var sockets = {}; - -function createSocket(path) { - if (sockets[path]) { - return sockets[path]; - } else { - var ws = new WebSocket('ws://' + window.location.host + path); - - var sock = { - send: function(type, data) { - ws.send(JSON.stringify({ type: type, request: data })); - } - }; - - _.extend(sock, EventEmitter.prototype); - - sockets[path] = sock; - - ws.onopen = function() { - sock.emit('connect'); - }; - - ws.onclose = function() { - sock.emit('disconnect'); - }; - - ws.onmessage = function(e) { - var msg = JSON.parse(e.data); - - sock.emit(msg.type, msg.response); - }; - - return sock; +var socket = { + send: function(type, data) { + ws.send(JSON.stringify({ type: type, request: data })); } -} +}; -module.exports = createSocket; \ No newline at end of file +_.extend(socket, EventEmitter.prototype); + +var ws = new WebSocket('ws://' + window.location.host + '/ws'); + +ws.onopen = function() { + socket.emit('connect'); +}; + +ws.onclose = function() { + socket.emit('disconnect'); +}; + +ws.onmessage = function(e) { + var msg = JSON.parse(e.data); + + socket.emit(msg.type, msg.response); +}; + +module.exports = socket; \ No newline at end of file diff --git a/client/src/js/stores/channel.js b/client/src/js/stores/channel.js index 24b57f56..68ba0eae 100644 --- a/client/src/js/stores/channel.js +++ b/client/src/js/stores/channel.js @@ -19,25 +19,25 @@ var channelStore = Reflux.createStore({ this.listenToMany(actions); }, - joined: function(user, server, channel) { + part: function(partChannels, server) { + _.each(partChannels, function(channel) { + delete channels[server][channel]; + }); + this.trigger(channels); + }, + + addUser: function(user, server, channel) { initChannel(server, channel); channels[server][channel].users.push(user); this.trigger(channels); }, - part: function(data) { - _.each(data.channels, function(channel) { - delete channels[data.server][channel]; - }); - this.trigger(channels); - }, - - parted: function(user, server, channel) { + removeUser: function(user, server, channel) { _.pull(channels[server][channel].users, user); this.trigger(channels); }, - quit: function(user, server) { + removeUserAll: function(user, server) { _.each(channels[server], function(channel) { _.pull(channel.users, user); }); diff --git a/client/src/js/stores/selectedTab.js b/client/src/js/stores/selectedTab.js index 2df937d8..876ae4eb 100644 --- a/client/src/js/stores/selectedTab.js +++ b/client/src/js/stores/selectedTab.js @@ -18,10 +18,10 @@ var selectedTabStore = Reflux.createStore({ this.trigger(selectedTab); }, - part: function(data) { + part: function(channels, server) { var self = this; - if (data.server === selectedTab.server) { - _.each(data.channels, function(channel) { + if (server === selectedTab.server) { + _.each(channels, function(channel) { if (channel === selectedTab.channel) { delete selectedTab.channel; self.trigger(selectedTab); diff --git a/client/src/js/stores/server.js b/client/src/js/stores/server.js index fba2c59a..29410551 100644 --- a/client/src/js/stores/server.js +++ b/client/src/js/stores/server.js @@ -9,8 +9,12 @@ var serverStore = Reflux.createStore({ this.listenToMany(actions); }, - connect: function(data) { - servers[data.server] = data; + connect: function(server, nick, username) { + servers[server] = { + server: server, + nick: nick, + username: username + }; this.trigger(servers); },