From 6c93bd13d1f6b55c98b9d642581ccd09e3f0d740 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 3 Aug 2020 15:43:20 +0200 Subject: [PATCH] Sort buffers when inserting, not when rendering This allows all state.buffers users to iterate over the list in the correct order. --- components/app.js | 24 ++++++++++++++++++++++-- components/buffer-list.js | 19 +------------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/components/app.js b/components/app.js index bc6874e..e4f8000 100644 --- a/components/app.js +++ b/components/app.js @@ -64,6 +64,24 @@ function debounce(f, delay) { }; } +function compareBuffers(a, b) { + if (a.type == BufferType.SERVER) { + return -1; + } + if (b.type == BufferType.SERVER) { + return 1; + } + + if (a.name > b.name) { + return -1; + } + if (a.name < b.name) { + return 1; + } + + return 0; +} + export default class App extends Component { client = null; state = { @@ -178,8 +196,8 @@ export default class App extends Component { type = BufferType.NICK; } - var buffers = new Map(state.buffers); - buffers.set(name, { + var bufferList = Array.from(state.buffers.values()); + bufferList.push({ name, type, serverInfo: null, // if server @@ -190,6 +208,8 @@ export default class App extends Component { messages: [], unread: Unread.NONE, }); + bufferList = bufferList.sort(compareBuffers); + var buffers = new Map(bufferList.map((buf) => [buf.name, buf])); return { buffers }; }); } diff --git a/components/buffer-list.js b/components/buffer-list.js index a51f352..d9d6cd9 100644 --- a/components/buffer-list.js +++ b/components/buffer-list.js @@ -26,28 +26,11 @@ function BufferItem(props) { `; } -function compareBuffers(a, b) { - if (a.type == BufferType.SERVER) { - return -1; - } - if (b.type == BufferType.SERVER) { - return 1; - } - - if (a.name > b.name) { - return -1; - } - if (a.name < b.name) { - return 1; - } - - return 0; -} export default function BufferList(props) { return html`