Append image property to all messages on UI side.

This commit is contained in:
Jannis Mattheis 2018-03-30 19:33:28 +02:00 committed by Jannis Mattheis
parent acc8c4d35f
commit 2b107ea51f
1 changed files with 11 additions and 0 deletions

View File

@ -1,11 +1,13 @@
import {EventEmitter} from 'events'; import {EventEmitter} from 'events';
import dispatcher from './dispatcher'; import dispatcher from './dispatcher';
import AppStore from './AppStore';
class MessageStore extends EventEmitter { class MessageStore extends EventEmitter {
constructor() { constructor() {
super(); super();
this.messages = []; this.messages = [];
this.messagesForApp = []; this.messagesForApp = [];
AppStore.on('change', this.updateApps);
} }
get() { get() {
@ -30,16 +32,25 @@ class MessageStore extends EventEmitter {
this.createIfNotExist(message.appid); this.createIfNotExist(message.appid);
this.messagesForApp[message.appid].push(message); this.messagesForApp[message.appid].push(message);
}.bind(this)); }.bind(this));
this.updateApps();
this.emit('change'); this.emit('change');
} else if (data.type === 'ONE_MESSAGE') { } else if (data.type === 'ONE_MESSAGE') {
const {payload} = data; const {payload} = data;
this.createIfNotExist(payload.appid); this.createIfNotExist(payload.appid);
this.messagesForApp[payload.appid].unshift(payload); this.messagesForApp[payload.appid].unshift(payload);
this.messages.unshift(payload); this.messages.unshift(payload);
this.updateApps();
this.emit('change'); 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) { createIfNotExist(id) {
if (!(id in this.messagesForApp)) { if (!(id in this.messagesForApp)) {
this.messagesForApp[id] = []; this.messagesForApp[id] = [];