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 { export default class App extends Component {
client = null; client = null;
state = { state = {
@ -178,8 +196,8 @@ export default class App extends Component {
type = BufferType.NICK; type = BufferType.NICK;
} }
var buffers = new Map(state.buffers); var bufferList = Array.from(state.buffers.values());
buffers.set(name, { bufferList.push({
name, name,
type, type,
serverInfo: null, // if server serverInfo: null, // if server
@ -190,6 +208,8 @@ export default class App extends Component {
messages: [], messages: [],
unread: Unread.NONE, unread: Unread.NONE,
}); });
bufferList = bufferList.sort(compareBuffers);
var buffers = new Map(bufferList.map((buf) => [buf.name, buf]));
return { buffers }; 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) { export default function BufferList(props) {
return html` return html`
<ul> <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}/> <${BufferItem} key=${buf.name} buffer=${buf} onClick=${() => props.onBufferClick(buf.name)} active=${props.activeBuffer == buf.name}/>
`)} `)}
</ul> </ul>