Append image property to all messages on UI side.
This commit is contained in:
parent
acc8c4d35f
commit
2b107ea51f
|
|
@ -1,11 +1,13 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
import AppStore from './AppStore';
|
||||
|
||||
class MessageStore extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.messages = [];
|
||||
this.messagesForApp = [];
|
||||
AppStore.on('change', this.updateApps);
|
||||
}
|
||||
|
||||
get() {
|
||||
|
|
@ -30,16 +32,25 @@ class MessageStore extends EventEmitter {
|
|||
this.createIfNotExist(message.appid);
|
||||
this.messagesForApp[message.appid].push(message);
|
||||
}.bind(this));
|
||||
this.updateApps();
|
||||
this.emit('change');
|
||||
} else if (data.type === 'ONE_MESSAGE') {
|
||||
const {payload} = data;
|
||||
this.createIfNotExist(payload.appid);
|
||||
this.messagesForApp[payload.appid].unshift(payload);
|
||||
this.messages.unshift(payload);
|
||||
this.updateApps();
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
|
||||
updateApps = () => {
|
||||
const appToUrl = {};
|
||||
AppStore.get().forEach((app) => appToUrl[app.id] = app.image);
|
||||
this.messages.forEach((msg) => msg.image = appToUrl[msg.appid]);
|
||||
this.messagesForApp.forEach((forApp) => forApp.forEach((msg) => msg.image = appToUrl[msg.appid]));
|
||||
};
|
||||
|
||||
createIfNotExist(id) {
|
||||
if (!(id in this.messagesForApp)) {
|
||||
this.messagesForApp[id] = [];
|
||||
|
|
|
|||
Loading…
Reference in New Issue