Don't sort the list on every render (#58)

This commit is contained in:
Maciej Hirsz
2018-09-27 17:01:51 +02:00
committed by GitHub
parent 87facf1552
commit 62d2847bb0
13 changed files with 416 additions and 67 deletions
+18
View File
@@ -35,8 +35,26 @@ export interface State {
subscribed: Maybe<Types.ChainLabel>;
chains: Map<Types.ChainLabel, Types.NodeCount>;
nodes: Map<Types.NodeId, State.Node>;
sortedNodes: State.Node[];
settings: Readonly<State.Settings>;
pins: Readonly<Set<Types.NodeName>>;
}
export type Update = <K extends keyof State>(changes: Pick<State, K> | null) => Readonly<State>;
export function compareNodes(a: State.Node, b: State.Node): number {
if (a.pinned === b.pinned) {
if (a.blockDetails[0] === b.blockDetails[0]) {
const aPropagation = a.blockDetails[4] == null ? Infinity : a.blockDetails[4] as number;
const bPropagation = b.blockDetails[4] == null ? Infinity : b.blockDetails[4] as number;
// Ascending sort by propagation time
return aPropagation - bPropagation;
}
} else {
return +b.pinned - +a.pinned;
}
// Descending sort by block number
return b.blockDetails[0] - a.blockDetails[0];
}