Skip to content

Commit bce2aad

Browse files
committed
Converts ThreadStore
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.
1 parent f4c7bb4 commit bce2aad

File tree

1 file changed

+59
-103
lines changed

1 file changed

+59
-103
lines changed
Lines changed: 59 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,83 @@
1-
/**
2-
* This file is provided by Facebook for testing and evaluation purposes
3-
* only. Facebook reserves all rights not expressly granted.
4-
*
5-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8-
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
9-
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
10-
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11-
*/
1+
var alt = require('../alt')
122

13-
var ChatAppDispatcher = require('../dispatcher/ChatAppDispatcher');
14-
var ChatConstants = require('../constants/ChatConstants');
15-
var ChatMessageUtils = require('../utils/ChatMessageUtils');
16-
var EventEmitter = require('events').EventEmitter;
17-
var merge = require('react/lib/merge');
3+
var ChatThreadActionCreators = require('../actions/ChatThreadActionCreators')
4+
var ChatServerActionCreators = require('../actions/ChatServerActionCreators')
5+
var ChatMessageUtils = require('../utils/ChatMessageUtils')
186

19-
var ActionTypes = ChatConstants.ActionTypes;
20-
var CHANGE_EVENT = 'change';
7+
class ThreadStore {
8+
constructor() {
9+
this.bindActions(ChatThreadActionCreators)
10+
this.bindActions(ChatServerActionCreators)
2111

22-
var _currentID = null;
23-
var _threads = {};
12+
this.currentID = null
13+
this.threads = {}
14+
}
2415

25-
var ThreadStore = merge(EventEmitter.prototype, {
16+
onClickThread(threadID) {
17+
this.currentID = threadID
18+
this.threads[this.currentID].lastMessage.isRead = true
19+
}
2620

27-
init: function(rawMessages) {
28-
rawMessages.forEach(function(message) {
29-
var threadID = message.threadID;
30-
var thread = _threads[threadID];
21+
onReceiveAll(rawMessages) {
22+
this._init(rawMessages)
23+
}
24+
25+
_init(rawMessages) {
26+
rawMessages.forEach((message) => {
27+
var threadID = message.threadID
28+
var thread = this.threads[threadID]
3129
if (thread && thread.lastTimestamp > message.timestamp) {
32-
return;
30+
return
3331
}
34-
_threads[threadID] = {
32+
this.threads[threadID] = {
3533
id: threadID,
3634
name: message.threadName,
37-
lastMessage: ChatMessageUtils.convertRawMessage(message, _currentID)
38-
};
39-
}, this);
35+
lastMessage: ChatMessageUtils.convertRawMessage(message, this.currentID)
36+
}
37+
})
4038

41-
if (!_currentID) {
42-
var allChrono = this.getAllChrono();
43-
_currentID = allChrono[allChrono.length - 1].id;
39+
if (!this.currentID) {
40+
var allChrono = this.getInstance().getAllChrono()
41+
this.currentID = allChrono[allChrono.length - 1].id
4442
}
4543

46-
_threads[_currentID].lastMessage.isRead = true;
47-
},
48-
49-
emitChange: function() {
50-
this.emit(CHANGE_EVENT);
51-
},
52-
53-
/**
54-
* @param {function} callback
55-
*/
56-
addChangeListener: function(callback) {
57-
this.on(CHANGE_EVENT, callback);
58-
},
59-
60-
/**
61-
* @param {function} callback
62-
*/
63-
removeChangeListener: function(callback) {
64-
this.removeListener(CHANGE_EVENT, callback);
65-
},
44+
this.threads[this.currentID].lastMessage.isRead = true
45+
}
6646

67-
/**
68-
* @param {string} id
69-
*/
70-
get: function(id) {
71-
return _threads[id];
72-
},
47+
static get(id) {
48+
return this.getState().threads[id]
49+
}
7350

74-
getAll: function() {
75-
return _threads;
76-
},
51+
static getAll() {
52+
return this.getState().threads
53+
}
7754

78-
getAllChrono: function() {
79-
var orderedThreads = [];
80-
for (var id in _threads) {
81-
var thread = _threads[id];
82-
orderedThreads.push(thread);
55+
static getAllChrono() {
56+
var { threads } = this.getState()
57+
var orderedThreads = []
58+
for (var id in threads) {
59+
var thread = threads[id]
60+
orderedThreads.push(thread)
8361
}
84-
orderedThreads.sort(function(a, b) {
62+
orderedThreads.sort((a, b) => {
8563
if (a.lastMessage.date < b.lastMessage.date) {
86-
return -1;
64+
return -1
8765
} else if (a.lastMessage.date > b.lastMessage.date) {
88-
return 1;
66+
return 1
8967
}
90-
return 0;
91-
});
92-
return orderedThreads;
93-
},
94-
95-
getCurrentID: function() {
96-
return _currentID;
97-
},
98-
99-
getCurrent: function() {
100-
return this.get(this.getCurrentID());
68+
return 0
69+
})
70+
return orderedThreads
10171
}
10272

103-
});
104-
105-
ThreadStore.dispatchToken = ChatAppDispatcher.register(function(payload) {
106-
var action = payload.action;
107-
108-
switch(action.type) {
109-
110-
case ActionTypes.CLICK_THREAD:
111-
_currentID = action.threadID;
112-
_threads[_currentID].lastMessage.isRead = true;
113-
ThreadStore.emitChange();
114-
break;
115-
116-
case ActionTypes.RECEIVE_RAW_MESSAGES:
117-
ThreadStore.init(action.rawMessages);
118-
ThreadStore.emitChange();
119-
break;
120-
121-
default:
122-
// do nothing
73+
static getCurrentID() {
74+
return this.getState().currentID
12375
}
12476

125-
});
77+
static getCurrent() {
78+
var { threads, currentID } = this.getState()
79+
return threads[currentID]
80+
}
81+
}
12682

127-
module.exports = ThreadStore;
83+
module.exports = alt.createStore(ThreadStore, 'ThreadStore')

0 commit comments

Comments
 (0)