Skip to content

Commit

Permalink
Converts MessageStore
Browse files Browse the repository at this point in the history
Stores are arguably the most complex piece in flux so we can convert
each in a single commit. We can start with MessageStore, a lot of the
basic internals of the code is left unchanged sans semicolons. The bulk
of the dispatcher logic has been moved inside the store class' action
listener methods that correspond to the proper action. Notably the
underscore variable `_messages` has been introduced as state inside our
store, and all the loose functions that existed are now static functions
of the class.
  • Loading branch information
goatslacker committed Dec 27, 2014
1 parent 58ea141 commit f4c7bb4
Showing 1 changed file with 64 additions and 112 deletions.
176 changes: 64 additions & 112 deletions examples/chat/js/stores/MessageStore.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,86 @@
/**
* This file is provided by Facebook for testing and evaluation purposes
* only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var alt = require('../alt')

var ChatThreadActionCreators = require('../actions/ChatThreadActionCreators')
var ChatServerActionCreators = require('../actions/ChatServerActionCreators')
var ChatMessageActionCreators = require('../actions/ChatMessageActionCreators')
var ThreadStore = require('../stores/ThreadStore')
var ChatMessageDataUtils = require('../utils/ChatMessageDataUtils')
var ChatMessageUtils = require('../utils/ChatMessageUtils')

class MessageStore {
constructor() {
this.bindActions(ChatThreadActionCreators)
this.bindActions(ChatMessageActionCreators)
this.bindActions(ChatServerActionCreators)

this.messages = {}
}

var ChatAppDispatcher = require('../dispatcher/ChatAppDispatcher');
var ChatConstants = require('../constants/ChatConstants');
var ChatMessageUtils = require('../utils/ChatMessageUtils');
var EventEmitter = require('events').EventEmitter;
var ThreadStore = require('../stores/ThreadStore');
var merge = require('react/lib/merge');
onCreateMessage(text) {
var message = ChatMessageDataUtils.getCreatedMessageData(text)
this.messages[message.id] = message
}

var ActionTypes = ChatConstants.ActionTypes;
var CHANGE_EVENT = 'change';
onReceiveAll(rawMessages) {
this._addMessages(rawMessages)
this.waitFor([ThreadStore.dispatchToken])
this._markAllInThreadRead(ThreadStore.getCurrentID())
}

var _messages = {};
onClickThread() {
this.waitFor([ThreadStore.dispatchToken])
this._markAllInThreadRead(ThreadStore.getCurrentID())
}

function _addMessages(rawMessages) {
rawMessages.forEach(function(message) {
if (!_messages[message.id]) {
_messages[message.id] = ChatMessageUtils.convertRawMessage(
message,
ThreadStore.getCurrentID()
);
}
});
}
_addMessages(rawMessages) {
rawMessages.forEach((message) => {
if (!this.messages[message.id]) {
this.messages[message.id] = ChatMessageUtils.convertRawMessage(
message,
ThreadStore.getCurrentID()
)
}
})
}

function _markAllInThreadRead(threadID) {
for (var id in _messages) {
if (_messages[id].threadID === threadID) {
_messages[id].isRead = true;
_markAllInThreadRead(threadID) {
for (var id in this.messages) {
if (this.messages[id].threadID === threadID) {
this.messages[id].isRead = true
}
}
}
}

var MessageStore = merge(EventEmitter.prototype, {

emitChange: function() {
this.emit(CHANGE_EVENT);
},

/**
* @param {function} callback
*/
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},

get: function(id) {
return _messages[id];
},

getAll: function() {
return _messages;
},

/**
* @param {string} threadID
*/
getAllForThread: function(threadID) {
var threadMessages = [];
static getAllForThread(threadID) {
var threadMessages = []
var _messages = this.getState().messages
for (var id in _messages) {
if (_messages[id].threadID === threadID) {
threadMessages.push(_messages[id]);
threadMessages.push(_messages[id])
}
}
threadMessages.sort(function(a, b) {
if (a.date < b.date) {
return -1;
return -1
} else if (a.date > b.date) {
return 1;
return 1
}
return 0;
});
return threadMessages;
},

getAllForCurrentThread: function() {
return this.getAllForThread(ThreadStore.getCurrentID());
},

getCreatedMessageData: function(text) {
var timestamp = Date.now();
return {
id: 'm_' + timestamp,
threadID: ThreadStore.getCurrentID(),
authorName: 'Bill', // hard coded for the example
date: new Date(timestamp),
text: text,
isRead: true
};
return 0
})
return threadMessages
}

});

MessageStore.dispatchToken = ChatAppDispatcher.register(function(payload) {
var action = payload.action;

switch(action.type) {

case ActionTypes.CLICK_THREAD:
ChatAppDispatcher.waitFor([ThreadStore.dispatchToken]);
_markAllInThreadRead(ThreadStore.getCurrentID());
MessageStore.emitChange();
break;

case ActionTypes.CREATE_MESSAGE:
var message = MessageStore.getCreatedMessageData(action.text);
_messages[message.id] = message;
MessageStore.emitChange();
break;

case ActionTypes.RECEIVE_RAW_MESSAGES:
_addMessages(action.rawMessages);
ChatAppDispatcher.waitFor([ThreadStore.dispatchToken]);
_markAllInThreadRead(ThreadStore.getCurrentID());
MessageStore.emitChange();
break;
static getAllForCurrentThread() {
return this.getAllForThread(ThreadStore.getCurrentID())
}

default:
// do nothing
static get(id) {
return this.getState().messages[id]
}

});
static getAll() {
return this.getState().messages
}
}

module.exports = MessageStore;
module.exports = alt.createStore(MessageStore, 'MessageStore')

0 comments on commit f4c7bb4

Please sign in to comment.