Added ago timers

This commit is contained in:
maciejhirsz
2018-07-05 17:04:00 +02:00
parent 81ef3ee14e
commit 01da7dfc47
19 changed files with 200 additions and 48 deletions
+11 -4
View File
@@ -1,5 +1,5 @@
import { Opaque } from './helpers';
import { NodeId, NodeDetails, NodeStats, BlockNumber, BlockDetails } from './types';
import { NodeId, NodeDetails, NodeStats, BlockNumber, BlockDetails, Timestamp } from './types';
export const Actions = {
BestBlock: 0 as 0,
@@ -7,6 +7,7 @@ export const Actions = {
RemovedNode: 2 as 2,
ImportedBlock: 3 as 3,
NodeStats: 4 as 4,
TimeSync: 5 as 5,
};
export type Action = typeof Actions[keyof typeof Actions];
@@ -19,7 +20,7 @@ export namespace Variants {
export interface BestBlockMessage extends MessageBase {
action: typeof Actions.BestBlock;
payload: BlockNumber;
payload: [BlockNumber, Timestamp];
}
export interface AddedNodeMessage extends MessageBase {
@@ -40,7 +41,12 @@ export namespace Variants {
export interface NodeStatsMessage extends MessageBase {
action: typeof Actions.NodeStats;
payload: [NodeId, NodeStats];
};
}
export interface TimeSyncMessage extends MessageBase {
action: typeof Actions.TimeSync;
payload: Timestamp;
}
}
export type Message =
@@ -48,7 +54,8 @@ export type Message =
| Variants.AddedNodeMessage
| Variants.RemovedNodeMessage
| Variants.ImportedBlockMessage
| Variants.NodeStatsMessage;
| Variants.NodeStatsMessage
| Variants.TimeSyncMessage;
/**
* Opaque data type to be sent to the feed. Passing through
+4 -2
View File
@@ -1,10 +1,10 @@
import { Milliseconds } from './types';
import { Milliseconds, Timestamp } from './types';
/**
* PhantomData akin to Rust, because sometimes you need to be smarter than
* the compiler.
*/
export abstract class PhantomData<P> { private __PHANTOM__: P }
export abstract class PhantomData<P> { public __PHANTOM__: P }
/**
* Opaque type, similar to `opaque type` in Flow, or new types in Rust/C.
@@ -31,3 +31,5 @@ export function sleep(time: Milliseconds): Promise<void> {
setTimeout(() => resolve(), time);
});
}
export const timestamp = Date.now as () => Timestamp;
+2 -1
View File
@@ -9,9 +9,10 @@ export type NodeVersion = Opaque<string, 'NodeVersion'>;
export type BlockNumber = Opaque<number, 'BlockNumber'>;
export type BlockHash = Opaque<string, 'BlockHash'>;
export type Milliseconds = Opaque<number, 'Milliseconds'>;
export type Timestamp = Opaque<Milliseconds, 'Timestamp'>;
export type PeerCount = Opaque<number, 'PeerCount'>;
export type TransactionCount = Opaque<number, 'TransactionCount'>;
export type BlockDetails = [BlockNumber, BlockHash, Milliseconds];
export type BlockDetails = [BlockNumber, BlockHash, Milliseconds, Timestamp];
export type NodeDetails = [NodeName, NodeImplementation, NodeVersion];
export type NodeStats = [PeerCount, TransactionCount];