Everything is immutable

This commit is contained in:
Ken-Håvard Lieng 2015-05-20 08:14:44 +02:00
parent ce72ec9c8c
commit a6d7403eba
11 changed files with 86 additions and 78 deletions

View File

@ -6,9 +6,11 @@ var Navigation = Router.Navigation;
var TabList = require('./TabList.jsx'); var TabList = require('./TabList.jsx');
var routeActions = require('../actions/route'); var routeActions = require('../actions/route');
var PureMixin = require('../mixins/pure');
var App = React.createClass({ var App = React.createClass({
mixins: [ mixins: [
PureMixin,
Navigation, Navigation,
Reflux.listenTo(routeActions.navigate, 'navigate') Reflux.listenTo(routeActions.navigate, 'navigate')
], ],

View File

@ -7,9 +7,11 @@ var serverActions = require('../actions/server');
var channelActions = require('../actions/channel'); var channelActions = require('../actions/channel');
var searchActions = require('../actions/search'); var searchActions = require('../actions/search');
var privateChatActions = require('../actions/privateChat'); var privateChatActions = require('../actions/privateChat');
var PureMixin = require('../mixins/pure');
var ChatTitle = React.createClass({ var ChatTitle = React.createClass({
mixins: [ mixins: [
PureMixin,
Reflux.listenTo(channelStore, 'channelsChanged'), Reflux.listenTo(channelStore, 'channelsChanged'),
Reflux.listenTo(selectedTabStore, 'selectedTabChanged') Reflux.listenTo(selectedTabStore, 'selectedTabChanged')
], ],

View File

@ -3,8 +3,11 @@ var _ = require('lodash');
var serverActions = require('../actions/server'); var serverActions = require('../actions/server');
var channelActions = require('../actions/channel'); var channelActions = require('../actions/channel');
var PureMixin = require('../mixins/pure');
var Connect = React.createClass({ var Connect = React.createClass({
mixins: [PureMixin],
getInitialState() { getInitialState() {
return { return {
showOptionals: false showOptionals: false

View File

@ -7,9 +7,11 @@ var channelStore = require('../stores/channel');
var privateChatStore = require('../stores/privateChat'); var privateChatStore = require('../stores/privateChat');
var serverStore = require('../stores/server'); var serverStore = require('../stores/server');
var routeActions = require('../actions/route'); var routeActions = require('../actions/route');
var PureMixin = require('../mixins/pure');
var TabList = React.createClass({ var TabList = React.createClass({
mixins: [ mixins: [
PureMixin,
Reflux.connect(serverStore, 'servers'), Reflux.connect(serverStore, 'servers'),
Reflux.connect(channelStore, 'channels'), Reflux.connect(channelStore, 'channels'),
Reflux.connect(privateChatStore, 'privateChats') Reflux.connect(privateChatStore, 'privateChats')
@ -32,36 +34,41 @@ var TabList = React.createClass({
}, },
render() { render() {
var tabs = this.state.channels.map((server, address) => { var tabs = [];
var serverTabs = server.map((channel, name) => {
return ( this.state.channels.forEach((server, address) => {
<TabListItem tabs.push(
<TabListItem
key={address}
server={address}
channel={null}
name={this.state.servers.getIn([address, 'name'])}>
</TabListItem>
);
server.forEach((channel, name) => {
tabs.push(
<TabListItem
key={address + name}
server={address} server={address}
channel={name} channel={name}
name={name}> name={name}>
</TabListItem> </TabListItem>
); );
}).toArray();
_.each(this.state.privateChats[address], (chat, nick) => {
serverTabs.push(
<TabListItem
server={address}
channel={nick}
name={nick}>
</TabListItem>
);
}); });
serverTabs.unshift( if (this.state.privateChats.has(address)) {
<TabListItem this.state.privateChats.get(address).forEach(nick => {
server={address} tabs.push(
channel={null} <TabListItem
name={serverStore.getName(address)}> key={address + nick}
</TabListItem> server={address}
); channel={nick}
name={nick}>
return serverTabs; </TabListItem>
);
});
}
}); });
return ( return (

View File

@ -6,9 +6,11 @@ var Infinite = require('react-infinite');
var UserListItem = require('./UserListItem.jsx'); var UserListItem = require('./UserListItem.jsx');
var channelStore = require('../stores/channel'); var channelStore = require('../stores/channel');
var selectedTabStore = require('../stores/selectedTab'); var selectedTabStore = require('../stores/selectedTab');
var PureMixin = require('../mixins/pure');
var UserList = React.createClass({ var UserList = React.createClass({
mixins: [ mixins: [
PureMixin,
Reflux.listenTo(channelStore, 'channelsChanged'), Reflux.listenTo(channelStore, 'channelsChanged'),
Reflux.listenTo(selectedTabStore, 'selectedTabChanged') Reflux.listenTo(selectedTabStore, 'selectedTabChanged')
], ],
@ -61,7 +63,7 @@ var UserList = React.createClass({
}).toArray(); // React Infinite uses .length }).toArray(); // React Infinite uses .length
} }
if (users.length !== 1) { if (users.length > 1) {
return ( return (
<div className="userlist" style={style}> <div className="userlist" style={style}>
<Infinite containerHeight={this.state.height} elementHeight={24}> <Infinite containerHeight={this.state.height} elementHeight={24}>

View File

@ -3,8 +3,11 @@ var React = require('react');
var selectedTabStore = require('../stores/selectedTab'); var selectedTabStore = require('../stores/selectedTab');
var privateChatActions = require('../actions/privateChat'); var privateChatActions = require('../actions/privateChat');
var tabActions = require('../actions/tab'); var tabActions = require('../actions/tab');
var PureMixin = require('../mixins/pure');
var UserListItem = React.createClass({ var UserListItem = React.createClass({
mixins: [PureMixin],
handleClick() { handleClick() {
var server = selectedTabStore.getServer(); var server = selectedTabStore.getServer();

View File

@ -39,13 +39,15 @@ function loadUser(nick) {
} }
function updateRenderName(user) { function updateRenderName(user) {
var name = user.nick;
if (user.mode.indexOf('o') !== -1) { if (user.mode.indexOf('o') !== -1) {
return user.set('renderName', '@' + user.nick); name = '@' + name;
} else if (user.mode.indexOf('v') !== -1) { } else if (user.mode.indexOf('v') !== -1) {
return user.set('renderName', '+' + user.nick); name = '+' + name;
} else {
return user.set('renderName', user.nick);
} }
return user.set('renderName', name);
} }
function sortUsers(a, b) { function sortUsers(a, b) {
@ -188,7 +190,7 @@ var channelStore = Reflux.createStore({
}, },
getTopic(server, channel) { getTopic(server, channel) {
return channels.getIn([server, channel, 'topic']) || null; return channels.getIn([server, channel, 'topic']);
}, },
getState() { getState() {

View File

@ -1,24 +1,12 @@
var Reflux = require('reflux'); var Reflux = require('reflux');
var Immutable = require('immutable');
var actions = require('../actions/privateChat'); var actions = require('../actions/privateChat');
var messageActions = require('../actions/message'); var messageActions = require('../actions/message');
var serverActions = require('../actions/server'); var serverActions = require('../actions/server');
var privateChats = {}; var privateChats = Immutable.Map();
var empty = Immutable.List();
function initChat(server, nick) {
if (!(server in privateChats)) {
privateChats[server] = {};
privateChats[server][nick] = {};
return true;
} else if (!(nick in privateChats[server])) {
privateChats[server][nick] = {};
return true;
}
return false;
}
var privateChatStore = Reflux.createStore({ var privateChatStore = Reflux.createStore({
init() { init() {
@ -28,13 +16,12 @@ var privateChatStore = Reflux.createStore({
}, },
open(server, nick) { open(server, nick) {
if (initChat(server, nick)) { privateChats = privateChats.update(server, empty, chats => chats.push(nick));
this.trigger(privateChats); this.trigger(privateChats);
}
}, },
close(server, nick) { close(server, nick) {
delete privateChats[server][nick]; privateChats = privateChats.update(server, chats => chats.delete(chats.indexOf(nick)));
this.trigger(privateChats); this.trigger(privateChats);
}, },
@ -45,7 +32,7 @@ var privateChatStore = Reflux.createStore({
}, },
disconnect(server) { disconnect(server) {
delete privateChats[server]; privateChats = privateChats.delete(server);
this.trigger(privateChats); this.trigger(privateChats);
}, },

View File

@ -3,28 +3,30 @@ var Immutable = require('immutable');
var actions = require('../actions/search'); var actions = require('../actions/search');
var state = Immutable.Map({ var Search = Immutable.Record({
show: false, show: false,
results: Immutable.List() results: Immutable.List()
}); });
var search = new Search();
var searchStore = Reflux.createStore({ var searchStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
}, },
searchDone(results) { searchDone(results) {
state = state.set('results', Immutable.List(results)); search = search.set('results', Immutable.List(results));
this.trigger(state); this.trigger(search);
}, },
toggle() { toggle() {
state = state.update('show', show => !show); search = search.set('show', !search.show);
this.trigger(state); this.trigger(search);
}, },
getState() { getState() {
return state; return search;
} }
}); });

View File

@ -1,10 +1,15 @@
var Reflux = require('reflux'); var Reflux = require('reflux');
var Immutable = require('immutable');
var _ = require('lodash'); var _ = require('lodash');
var actions = require('../actions/server'); var actions = require('../actions/server');
var tabActions = require('../actions/tab'); var tabActions = require('../actions/tab');
var servers = {}; var servers = Immutable.Map();
var Server = Immutable.Record({
nick: null,
name: null
});
var serverStore = Reflux.createStore({ var serverStore = Reflux.createStore({
init() { init() {
@ -17,45 +22,38 @@ var serverStore = Reflux.createStore({
server = server.slice(0, i); server = server.slice(0, i);
} }
servers[server] = { servers = servers.set(server, new Server({
address: server,
nick: nick, nick: nick,
name: opts.name || server name: opts.name || server
}; }));
this.trigger(servers); this.trigger(servers);
tabActions.select(server); tabActions.select(server);
}, },
disconnect(server) { disconnect(server) {
delete servers[server]; servers = servers.delete(server);
this.trigger(servers); this.trigger(servers);
}, },
setNick(nick, server) { setNick(nick, server) {
servers[server].nick = nick; servers = servers.update(server, s => s.set('nick', nick));
this.trigger(servers); this.trigger(servers);
}, },
load(storedServers) { load(storedServers) {
_.each(storedServers, function(server) { _.each(storedServers, function(server) {
servers[server.address] = server; servers = servers.set(server.address, new Server(server));
}); });
this.trigger(servers); this.trigger(servers);
}, },
getNick(server) { getNick(server) {
if (servers[server]) { return servers.getIn([server, 'nick']);
return servers[server].nick;
}
return null;
}, },
getName(server) { getName(server) {
if (servers[server]) { return servers.getIn([server, 'name']);
return servers[server].name;
}
return null;
}, },
getState() { getState() {

File diff suppressed because one or more lines are too long