Cleaned up action naming and handling

This commit is contained in:
khlieng 2015-01-22 00:14:28 +01:00
parent e1cb8c468d
commit 43a725c662
9 changed files with 129 additions and 139 deletions

View File

@ -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);
});

View File

@ -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;

View File

@ -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;

View File

@ -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(<App />, 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
});
});
});
React.render(<App />, document.body);

View File

@ -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;
}

View File

@ -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;
_.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;

View File

@ -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);
});

View File

@ -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);

View File

@ -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);
},