Skip to content

Commit

Permalink
Converts ThreadStore
Browse files Browse the repository at this point in the history
Next step is to convert ThreadStore. The main differences are that
currentId and threads are now part of the instance. There is this class
method `_init` which isn't bound to an action, it is keeping in spirit
with the original flux version. Inside `_init` there is a call to
`this.getInstance()` what that does is it gives you the store's instance
so you can call the static methods or use any of the store instance's
functions. The static methods then have access to `this.getState()`
which they use to return the correct data.
  • Loading branch information
goatslacker committed Dec 27, 2014
1 parent f4c7bb4 commit bce2aad
Showing 1 changed file with 59 additions and 103 deletions.
162 changes: 59 additions & 103 deletions examples/chat/js/stores/ThreadStore.js
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit bce2aad

Please sign in to comment.