mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-06-14 09:31:01 +00:00
Reformatting
This commit is contained in:
+81
-81
@@ -2,88 +2,88 @@ import { Opaque } from './helpers';
|
||||
import { NodeId, NodeDetails, NodeStats, BlockNumber, BlockDetails, Timestamp, ChainLabel } from './types';
|
||||
|
||||
export const Actions = {
|
||||
BestBlock: 0 as 0,
|
||||
AddedNode: 1 as 1,
|
||||
RemovedNode: 2 as 2,
|
||||
ImportedBlock: 3 as 3,
|
||||
NodeStats: 4 as 4,
|
||||
TimeSync: 5 as 5,
|
||||
AddedChain: 6 as 6,
|
||||
RemovedChain: 7 as 7,
|
||||
SubscribedTo: 8 as 8,
|
||||
UnsubscribedFrom: 9 as 9
|
||||
BestBlock: 0 as 0,
|
||||
AddedNode: 1 as 1,
|
||||
RemovedNode: 2 as 2,
|
||||
ImportedBlock: 3 as 3,
|
||||
NodeStats: 4 as 4,
|
||||
TimeSync: 5 as 5,
|
||||
AddedChain: 6 as 6,
|
||||
RemovedChain: 7 as 7,
|
||||
SubscribedTo: 8 as 8,
|
||||
UnsubscribedFrom: 9 as 9
|
||||
};
|
||||
|
||||
export type Action = typeof Actions[keyof typeof Actions];
|
||||
export type Payload = Message['payload'];
|
||||
|
||||
export namespace Variants {
|
||||
export interface MessageBase {
|
||||
action: Action;
|
||||
}
|
||||
export interface MessageBase {
|
||||
action: Action;
|
||||
}
|
||||
|
||||
export interface BestBlockMessage extends MessageBase {
|
||||
action: typeof Actions.BestBlock;
|
||||
payload: [BlockNumber, Timestamp];
|
||||
}
|
||||
export interface BestBlockMessage extends MessageBase {
|
||||
action: typeof Actions.BestBlock;
|
||||
payload: [BlockNumber, Timestamp];
|
||||
}
|
||||
|
||||
export interface AddedNodeMessage extends MessageBase {
|
||||
action: typeof Actions.AddedNode;
|
||||
payload: [NodeId, NodeDetails, NodeStats, BlockDetails];
|
||||
}
|
||||
export interface AddedNodeMessage extends MessageBase {
|
||||
action: typeof Actions.AddedNode;
|
||||
payload: [NodeId, NodeDetails, NodeStats, BlockDetails];
|
||||
}
|
||||
|
||||
export interface RemovedNodeMessage extends MessageBase {
|
||||
action: typeof Actions.RemovedNode;
|
||||
payload: NodeId;
|
||||
}
|
||||
export interface RemovedNodeMessage extends MessageBase {
|
||||
action: typeof Actions.RemovedNode;
|
||||
payload: NodeId;
|
||||
}
|
||||
|
||||
export interface ImportedBlockMessage extends MessageBase {
|
||||
action: typeof Actions.ImportedBlock;
|
||||
payload: [NodeId, BlockDetails];
|
||||
}
|
||||
export interface ImportedBlockMessage extends MessageBase {
|
||||
action: typeof Actions.ImportedBlock;
|
||||
payload: [NodeId, BlockDetails];
|
||||
}
|
||||
|
||||
export interface NodeStatsMessage extends MessageBase {
|
||||
action: typeof Actions.NodeStats;
|
||||
payload: [NodeId, NodeStats];
|
||||
}
|
||||
export interface NodeStatsMessage extends MessageBase {
|
||||
action: typeof Actions.NodeStats;
|
||||
payload: [NodeId, NodeStats];
|
||||
}
|
||||
|
||||
export interface TimeSyncMessage extends MessageBase {
|
||||
action: typeof Actions.TimeSync;
|
||||
payload: Timestamp;
|
||||
}
|
||||
export interface TimeSyncMessage extends MessageBase {
|
||||
action: typeof Actions.TimeSync;
|
||||
payload: Timestamp;
|
||||
}
|
||||
|
||||
export interface AddedChainMessage extends MessageBase {
|
||||
action: typeof Actions.AddedChain;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
export interface AddedChainMessage extends MessageBase {
|
||||
action: typeof Actions.AddedChain;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
|
||||
export interface RemovedChainMessage extends MessageBase {
|
||||
action: typeof Actions.RemovedChain;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
export interface RemovedChainMessage extends MessageBase {
|
||||
action: typeof Actions.RemovedChain;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
|
||||
export interface SubscribedToMessage extends MessageBase {
|
||||
action: typeof Actions.SubscribedTo;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
export interface SubscribedToMessage extends MessageBase {
|
||||
action: typeof Actions.SubscribedTo;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
|
||||
export interface UnsubscribedFromMessage extends MessageBase {
|
||||
action: typeof Actions.UnsubscribedFrom;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
export interface UnsubscribedFromMessage extends MessageBase {
|
||||
action: typeof Actions.UnsubscribedFrom;
|
||||
payload: ChainLabel;
|
||||
}
|
||||
}
|
||||
|
||||
export type Message =
|
||||
| Variants.BestBlockMessage
|
||||
| Variants.AddedNodeMessage
|
||||
| Variants.RemovedNodeMessage
|
||||
| Variants.ImportedBlockMessage
|
||||
| Variants.NodeStatsMessage
|
||||
| Variants.TimeSyncMessage
|
||||
| Variants.AddedChainMessage
|
||||
| Variants.RemovedChainMessage
|
||||
| Variants.SubscribedToMessage
|
||||
| Variants.UnsubscribedFromMessage;
|
||||
| Variants.BestBlockMessage
|
||||
| Variants.AddedNodeMessage
|
||||
| Variants.RemovedNodeMessage
|
||||
| Variants.ImportedBlockMessage
|
||||
| Variants.NodeStatsMessage
|
||||
| Variants.TimeSyncMessage
|
||||
| Variants.AddedChainMessage
|
||||
| Variants.RemovedChainMessage
|
||||
| Variants.SubscribedToMessage
|
||||
| Variants.UnsubscribedFromMessage;
|
||||
|
||||
/**
|
||||
* Opaque data type to be sent to the feed. Passing through
|
||||
@@ -100,36 +100,36 @@ export type Data = Opaque<string, 'FeedMessage.Data'>;
|
||||
* Action `string`s are converted to opcodes using the `actionToCode` mapping.
|
||||
*/
|
||||
export function serialize(messages: Array<Message>): Data {
|
||||
const squashed = new Array(messages.length * 2);
|
||||
let index = 0;
|
||||
const squashed = new Array(messages.length * 2);
|
||||
let index = 0;
|
||||
|
||||
messages.forEach((message) => {
|
||||
const { action, payload } = message;
|
||||
messages.forEach((message) => {
|
||||
const { action, payload } = message;
|
||||
|
||||
squashed[index++] = action;
|
||||
squashed[index++] = payload;
|
||||
})
|
||||
squashed[index++] = action;
|
||||
squashed[index++] = payload;
|
||||
})
|
||||
|
||||
return JSON.stringify(squashed) as Data;
|
||||
return JSON.stringify(squashed) as Data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize data to an array of `Message`s.
|
||||
*/
|
||||
export function deserialize(data: Data): Array<Message> {
|
||||
const json: Array<Action | Payload> = JSON.parse(data);
|
||||
const json: Array<Action | Payload> = JSON.parse(data);
|
||||
|
||||
if (!Array.isArray(json) || json.length === 0 || json.length % 2 !== 0) {
|
||||
throw new Error('Invalid FeedMessage.Data');
|
||||
}
|
||||
if (!Array.isArray(json) || json.length === 0 || json.length % 2 !== 0) {
|
||||
throw new Error('Invalid FeedMessage.Data');
|
||||
}
|
||||
|
||||
const messages: Array<Message> = new Array(json.length / 2);
|
||||
const messages: Array<Message> = new Array(json.length / 2);
|
||||
|
||||
for (const index of messages.keys()) {
|
||||
const [ action, payload ] = json.slice(index * 2);
|
||||
for (const index of messages.keys()) {
|
||||
const [ action, payload ] = json.slice(index * 2);
|
||||
|
||||
messages[index] = { action, payload } as Message;
|
||||
}
|
||||
messages[index] = { action, payload } as Message;
|
||||
}
|
||||
|
||||
return messages;
|
||||
return messages;
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ export type Maybe<T> = T | null | undefined;
|
||||
* Asynchronous sleep
|
||||
*/
|
||||
export function sleep(time: Milliseconds): Promise<void> {
|
||||
return new Promise<void>((resolve, _reject) => {
|
||||
setTimeout(() => resolve(), time);
|
||||
});
|
||||
return new Promise<void>((resolve, _reject) => {
|
||||
setTimeout(() => resolve(), time);
|
||||
});
|
||||
}
|
||||
|
||||
export const timestamp = Date.now as () => Timestamp;
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
export function* map<T, U>(iter: IterableIterator<T>, fn: (item: T) => U): IterableIterator<U> {
|
||||
for (const item of iter) {
|
||||
yield fn(item);
|
||||
}
|
||||
for (const item of iter) {
|
||||
yield fn(item);
|
||||
}
|
||||
}
|
||||
|
||||
export function* chain<T>(a: IterableIterator<T>, b: IterableIterator<T>): IterableIterator<T> {
|
||||
yield* a;
|
||||
yield* b;
|
||||
yield* a;
|
||||
yield* b;
|
||||
}
|
||||
|
||||
export function* zip<T, U>(a: IterableIterator<T>, b: IterableIterator<U>): IterableIterator<[T, U]> {
|
||||
let itemA = a.next();
|
||||
let itemB = b.next();
|
||||
let itemA = a.next();
|
||||
let itemB = b.next();
|
||||
|
||||
while (!itemA.done && !itemB.done) {
|
||||
yield [itemA.value, itemB.value];
|
||||
while (!itemA.done && !itemB.done) {
|
||||
yield [itemA.value, itemB.value];
|
||||
|
||||
itemA = a.next();
|
||||
itemB = b.next();
|
||||
}
|
||||
itemA = a.next();
|
||||
itemB = b.next();
|
||||
}
|
||||
}
|
||||
|
||||
export function* take<T>(iter: IterableIterator<T>, n: number): IterableIterator<T> {
|
||||
for (const item of iter) {
|
||||
if (n-- === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
yield item;
|
||||
for (const item of iter) {
|
||||
if (n-- === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
yield item;
|
||||
}
|
||||
}
|
||||
|
||||
export function skip<T>(iter: IterableIterator<T>, n: number): IterableIterator<T> {
|
||||
while (n-- !== 0 && !iter.next().done) {}
|
||||
while (n-- !== 0 && !iter.next().done) {}
|
||||
|
||||
return iter;
|
||||
return iter;
|
||||
}
|
||||
|
||||
export function reduce<T, R>(iter: IterableIterator<T>, fn: (accu: R, item: T) => R, accumulator: R): R {
|
||||
for (const item of iter) {
|
||||
accumulator = fn(accumulator, item);
|
||||
}
|
||||
for (const item of iter) {
|
||||
accumulator = fn(accumulator, item);
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
export function join(iter: IterableIterator<{ toString: () => string }>, glue: string): string {
|
||||
const first = iter.next();
|
||||
const first = iter.next();
|
||||
|
||||
if (first.done) {
|
||||
return '';
|
||||
}
|
||||
if (first.done) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let result = first.value.toString();
|
||||
let result = first.value.toString();
|
||||
|
||||
for (const item of iter) {
|
||||
result += glue + item;
|
||||
}
|
||||
for (const item of iter) {
|
||||
result += glue + item;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user