export class Messages { constructor() { this.channels = {}; } inChannel(channel) { return this.channels[channel] || []; } addMessage(channel, at, sender, body) { this.updateChannel(channel, (messages) => [...messages, { at, sender, body }]); return this; } addMessages(channel, payloads) { this.updateChannel(channel, (messages) => [...messages, ...payloads]); return this; } deleteMessage(message) { for (let channel in this.channels) { this.updateChannel(channel, (messages) => messages.filter((msg) => msg.id != message)); } return this; } deleteChannel(id) { delete this.channels[id]; return this; } updateChannel(channel, callback) { let messages = callback(this.inChannel(channel)); messages.sort((a, b) => a.at - b.at); this.channels[channel] = messages; } }