diff --git a/components/app.js b/components/app.js index f9e1b05..d3a604b 100644 --- a/components/app.js +++ b/components/app.js @@ -77,29 +77,6 @@ function fillConnectParams(params) { return params; } -/* Insert a message in an immutable list of sorted messages. */ -function insertMessage(list, msg) { - if (list.length == 0) { - return [msg]; - } else if (list[list.length - 1].tags.time <= msg.tags.time) { - return list.concat(msg); - } - - var insertBefore = -1; - for (var i = 0; i < list.length; i++) { - var other = list[i]; - if (msg.tags.time < other.tags.time) { - insertBefore = i; - break; - } - } - console.assert(insertBefore >= 0, ""); - - list = [ ...list ]; - list.splice(insertBefore, 0, msg); - return list; -} - function debounce(f, delay) { var timeout = null; return (...args) => { @@ -417,7 +394,9 @@ export default class App extends Component { this.setReceipt(bufName, ReceiptType.DELIVERED, msg); - this.setBufferState({ server: serverID, name: bufName }, (buf) => { + var bufID = { server: serverID, name: bufName }; + this.setState((state) => State.addMessage(state, msg, bufID)); + this.setBufferState(bufID, (buf) => { // TODO: set unread if scrolled up var unread = buf.unread; var lastReadReceipt = buf.lastReadReceipt; @@ -427,8 +406,7 @@ export default class App extends Component { this.setReceipt(bufName, ReceiptType.READ, msg); lastReadReceipt = this.getReceipt(bufName, ReceiptType.READ); } - var messages = insertMessage(buf.messages, msg); - return { messages, unread, lastReadReceipt }; + return { unread, lastReadReceipt }; }); } diff --git a/state.js b/state.js index d9f1043..925409a 100644 --- a/state.js +++ b/state.js @@ -111,6 +111,29 @@ function compareBuffers(a, b) { return 0; } +/* Insert a message in an immutable list of sorted messages. */ +function insertMessage(list, msg) { + if (list.length == 0) { + return [msg]; + } else if (list[list.length - 1].tags.time <= msg.tags.time) { + return list.concat(msg); + } + + var insertBefore = -1; + for (var i = 0; i < list.length; i++) { + var other = list[i]; + if (msg.tags.time < other.tags.time) { + insertBefore = i; + break; + } + } + console.assert(insertBefore >= 0, ""); + + list = [ ...list ]; + list.splice(insertBefore, 0, msg); + return list; +} + var lastBufferID = 0; export const State = { @@ -345,4 +368,10 @@ export const State = { return updateBuffer(channel, { topic }); } }, + addMessage(state, msg, bufID) { + return State.updateBuffer(state, bufID, (buf) => { + var messages = insertMessage(buf.messages, msg); + return { messages }; + }); + }, };