fix: Relates to #9. Reuse calculation of average block time

Issues:

* Unable to import `blockAverage` into Chain.ts or Node.ts due to Issue #21.
* TypeScript says `[ts] Module '"/Users/Me/code/blockchain/clones/paritytech/dotstats/packages/common/build/index"' has no exported member 'blockAverage'.`
This commit is contained in:
Luke Schoen
2018-07-25 08:13:06 +02:00
parent 20d8b21adc
commit ee8b76233a
4 changed files with 26 additions and 29 deletions
+4 -17
View File
@@ -1,6 +1,7 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import { noop, timestamp, Maybe, Types, idGenerator } from '@dotstats/common';
import { noop, timestamp, Maybe, Types, idGenerator, blockAverage } from '@dotstats/common';
import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message';
import { locate, Location } from './location';
@@ -176,22 +177,8 @@ export default class Node {
return location ? [location.lat, location.lon, location.city] : null;
}
public get average(): number {
let accounted = 0;
let sum = 0;
for (const time of this.blockTimes) {
if (time) {
accounted += 1;
sum += time;
}
}
if (accounted === 0) {
return 0;
}
return sum / accounted;
public get average(): Types.Milliseconds {
return blockAverage(this.blockTimes);
}
public get localBlockAt(): Types.Milliseconds {