Sort buffers when inserting, not when rendering

This allows all state.buffers users to iterate over the list in the
correct order.
This commit is contained in:
Simon Ser 2020-08-03 15:43:20 +02:00
parent ee8b40aae4
commit 6c93bd13d1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 23 additions and 20 deletions

View File

@ -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 };
});
}

View File

@ -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`
<ul>
${Array.from(props.buffers.values()).sort(compareBuffers).map(buf => html`
${Array.from(props.buffers.values()).map((buf) => html`
<${BufferItem} key=${buf.name} buffer=${buf} onClick=${() => props.onBufferClick(buf.name)} active=${props.activeBuffer == buf.name}/>
`)}
</ul>