diff --git a/examples/chat/js/stores/ThreadStore.js b/examples/chat/js/stores/ThreadStore.js index 6910e569..b36e6cc6 100644 --- a/examples/chat/js/stores/ThreadStore.js +++ b/examples/chat/js/stores/ThreadStore.js @@ -1,127 +1,83 @@ -/** - * 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 ChatAppDispatcher = require('../dispatcher/ChatAppDispatcher'); -var ChatConstants = require('../constants/ChatConstants'); -var ChatMessageUtils = require('../utils/ChatMessageUtils'); -var EventEmitter = require('events').EventEmitter; -var merge = require('react/lib/merge'); +var ChatThreadActionCreators = require('../actions/ChatThreadActionCreators') +var ChatServerActionCreators = require('../actions/ChatServerActionCreators') +var ChatMessageUtils = require('../utils/ChatMessageUtils') -var ActionTypes = ChatConstants.ActionTypes; -var CHANGE_EVENT = 'change'; +class ThreadStore { + constructor() { + this.bindActions(ChatThreadActionCreators) + this.bindActions(ChatServerActionCreators) -var _currentID = null; -var _threads = {}; + this.currentID = null + this.threads = {} + } -var ThreadStore = merge(EventEmitter.prototype, { + onClickThread(threadID) { + this.currentID = threadID + this.threads[this.currentID].lastMessage.isRead = true + } - init: function(rawMessages) { - rawMessages.forEach(function(message) { - var threadID = message.threadID; - var thread = _threads[threadID]; + onReceiveAll(rawMessages) { + this._init(rawMessages) + } + + _init(rawMessages) { + rawMessages.forEach((message) => { + var threadID = message.threadID + var thread = this.threads[threadID] if (thread && thread.lastTimestamp > message.timestamp) { - return; + return } - _threads[threadID] = { + this.threads[threadID] = { id: threadID, name: message.threadName, - lastMessage: ChatMessageUtils.convertRawMessage(message, _currentID) - }; - }, this); + lastMessage: ChatMessageUtils.convertRawMessage(message, this.currentID) + } + }) - if (!_currentID) { - var allChrono = this.getAllChrono(); - _currentID = allChrono[allChrono.length - 1].id; + if (!this.currentID) { + var allChrono = this.getInstance().getAllChrono() + this.currentID = allChrono[allChrono.length - 1].id } - _threads[_currentID].lastMessage.isRead = true; - }, - - emitChange: function() { - this.emit(CHANGE_EVENT); - }, - - /** - * @param {function} callback - */ - addChangeListener: function(callback) { - this.on(CHANGE_EVENT, callback); - }, - - /** - * @param {function} callback - */ - removeChangeListener: function(callback) { - this.removeListener(CHANGE_EVENT, callback); - }, + this.threads[this.currentID].lastMessage.isRead = true + } - /** - * @param {string} id - */ - get: function(id) { - return _threads[id]; - }, + static get(id) { + return this.getState().threads[id] + } - getAll: function() { - return _threads; - }, + static getAll() { + return this.getState().threads + } - getAllChrono: function() { - var orderedThreads = []; - for (var id in _threads) { - var thread = _threads[id]; - orderedThreads.push(thread); + static getAllChrono() { + var { threads } = this.getState() + var orderedThreads = [] + for (var id in threads) { + var thread = threads[id] + orderedThreads.push(thread) } - orderedThreads.sort(function(a, b) { + orderedThreads.sort((a, b) => { if (a.lastMessage.date < b.lastMessage.date) { - return -1; + return -1 } else if (a.lastMessage.date > b.lastMessage.date) { - return 1; + return 1 } - return 0; - }); - return orderedThreads; - }, - - getCurrentID: function() { - return _currentID; - }, - - getCurrent: function() { - return this.get(this.getCurrentID()); + return 0 + }) + return orderedThreads } -}); - -ThreadStore.dispatchToken = ChatAppDispatcher.register(function(payload) { - var action = payload.action; - - switch(action.type) { - - case ActionTypes.CLICK_THREAD: - _currentID = action.threadID; - _threads[_currentID].lastMessage.isRead = true; - ThreadStore.emitChange(); - break; - - case ActionTypes.RECEIVE_RAW_MESSAGES: - ThreadStore.init(action.rawMessages); - ThreadStore.emitChange(); - break; - - default: - // do nothing + static getCurrentID() { + return this.getState().currentID } -}); + static getCurrent() { + var { threads, currentID } = this.getState() + return threads[currentID] + } +} -module.exports = ThreadStore; +module.exports = alt.createStore(ThreadStore, 'ThreadStore')