Add shared folder

This commit is contained in:
maciejhirsz
2018-06-27 10:39:31 +02:00
parent 462cb8404f
commit 07a793cbb2
7 changed files with 6 additions and 5 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
import * as EventEmitter from 'events';
import Node from './node';
import Feed, { FeedData } from './feed';
import { Id, IdSet } from './utils';
import { Id, IdSet } from './shared';
export default class Aggregator extends EventEmitter {
private nodes: IdSet<Node> = new IdSet<Node>();
+1 -1
View File
@@ -1,7 +1,7 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import Node, { NodeInfo, BlockInfo } from './node';
import { Opaque, Id, idGenerator } from './utils';
import { Opaque, Id, idGenerator } from './shared';
const nextId = idGenerator<Feed>();
+1 -1
View File
@@ -4,7 +4,7 @@ import { createServer } from 'http';
import Node from './node';
import Feed from './feed';
import Aggregator from './aggregator';
import { map, join } from './utils';
import { map, join } from './shared';
const aggregator = new Aggregator;
const app = express();
+1 -1
View File
@@ -1,5 +1,5 @@
import { Data } from 'ws';
import { Maybe, Opaque } from './utils';
import { Maybe, Opaque } from './shared';
export function parseMessage(data: Data): Maybe<Message> {
try {
+1 -1
View File
@@ -1,6 +1,6 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import { Maybe, Id, idGenerator } from './utils';
import { Maybe, Id, idGenerator } from './shared';
import { parseMessage, getBestBlock, Message, BestBlock } from './message';
const BLOCK_TIME_HISTORY = 10;
+1
View File
@@ -0,0 +1 @@
../../shared
-109
View File
@@ -1,109 +0,0 @@
/**
* PhantomData akin to Rust, because sometimes you need to be smarter than
* the compiler.
*/
export class PhantomData<P> { private __PHANTOM__: P }
/**
* Opaque type, similar to `opaque type` in Flow, or new types in Rust/C.
* These should be produced only by manually casting `t as Opaque<T, P>`.
*
* `P` can be anything as it's never actually used. Using strings is okay:
*
* ```
* type MyType = Opaque<number, 'MyType'>;
* ```
*/
export type Opaque<T, P> = T & PhantomData<P>;
/**
* Just a readable shorthand for null-ish-able types, akin to `T?` in Flow.
*/
export type Maybe<T> = T | null | undefined;
/**
* Higher order function producing new auto-incremented `Id`s.
*/
export function idGenerator<T>(): () => Id<T> {
let current = 0;
return () => current++ as Id<T>;
}
/**
* Unique type-constrained Id number.
*/
export type Id<T> = Opaque<number, T>;
interface HasId<T> {
id: Id<T>;
}
export class IdSet<T> {
private map: Map<Id<T>, T> = new Map();
public add(item: T & HasId<T>) {
this.map.set(item.id, item);
}
public remove(item: T & HasId<T>) {
this.map.delete(item.id);
}
public get entries(): IterableIterator<T> {
return this.map.values();
}
}
export function* map<T, U>(iter: IterableIterator<T>, fn: (item: T) => U): IterableIterator<U> {
for (const item of iter) yield fn(item);
}
export function* chain<T>(a: IterableIterator<T>, b: IterableIterator<T>): IterableIterator<T> {
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();
while (!itemA.done && !itemB.done) {
yield [itemA.value, itemB.value];
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;
}
}
export function skip<T>(iter: IterableIterator<T>, n: number): IterableIterator<T> {
while (n-- !== 0 && !iter.next().done) {}
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);
return accumulator;
}
export function join(iter: IterableIterator<{ toString: () => string }>, glue: string): string {
const first = iter.next();
if (first.done) return '';
let result = first.value.toString();
for (const item of iter) result += glue + item;
return result;
}