Sort chains by node count

This commit is contained in:
maciejhirsz
2018-07-12 16:04:04 +02:00
parent 47b80ad30e
commit ea8d7ad77d
10 changed files with 63 additions and 25 deletions
+4 -2
View File
@@ -16,13 +16,15 @@ export default class Aggregator {
let chain = this.getChain(node.chain);
chain.addNode(node);
this.feeds.broadcast(Feed.addedChain(chain));
}
public addFeed(feed: Feed) {
this.feeds.add(feed);
for (const chain of this.chains.values()) {
feed.sendMessage(Feed.addedChain(chain.label));
feed.sendMessage(Feed.addedChain(chain));
}
feed.events.on('subscribe', (label: Types.ChainLabel) => {
@@ -68,7 +70,7 @@ export default class Aggregator {
this.chains.set(label, chain);
console.log(`New chain: ${label}`);
this.feeds.broadcast(Feed.addedChain(label));
this.feeds.broadcast(Feed.addedChain(chain));
return chain;
}
+2 -2
View File
@@ -18,8 +18,8 @@ export default class Chain {
this.label = label;
}
public get nodeCount(): number {
return this.nodes.size;
public get nodeCount(): Types.NodeCount {
return this.nodes.size as Types.NodeCount;
}
public addNode(node: Node) {
+3 -2
View File
@@ -1,6 +1,7 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import Node from './Node';
import Chain from './Chain';
import { timestamp, Maybe, FeedMessage, Types, idGenerator } from '@dotstats/common';
const nextId = idGenerator<Types.FeedId>();
@@ -66,10 +67,10 @@ export default class Feed {
};
}
public static addedChain(label: Types.ChainLabel): FeedMessage.Message {
public static addedChain(chain: Chain): FeedMessage.Message {
return {
action: Actions.AddedChain,
payload: label
payload: [chain.label, chain.nodeCount]
};
}