From 003ed008e98970de45f92a25d5a90f2c8da61f4f Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Wed, 27 Jun 2018 11:00:53 +0200 Subject: [PATCH] Added frontend to the repo --- .gitignore | 26 +++++++++- backend/package.json | 15 +++--- backend/tsconfig.json | 2 +- frontend | 1 + shared/id.ts | 35 +++++++++++++ shared/index.ts | 112 ++---------------------------------------- shared/iterators.ts | 62 +++++++++++++++++++++++ shared/types.ts | 22 +++++++++ 8 files changed, 156 insertions(+), 119 deletions(-) create mode 160000 frontend create mode 100644 shared/id.ts create mode 100644 shared/iterators.ts create mode 100644 shared/types.ts diff --git a/.gitignore b/.gitignore index f06235c..479d2f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,24 @@ -node_modules -dist +# See https://help.github.com/ignore-files/ for more about ignoring files. + +# dependencies +/backend/node_modules +/frontend/node_modules + +# testing +/backend/coverage +/frontend/coverage + +# production +/backend/build +/frontend/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/backend/package.json b/backend/package.json index e50e827..64fcf17 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,14 +1,15 @@ { - "name": "dotstats-server", + "name": "dotstats-backend", "version": "0.1.0", - "main": "src/index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "tsc && node dist/index.js", - "tsc": "tsc" - }, "author": "Parity Technologies Ltd. ", "license": "GPL-3.0", + "description": "Polkadot Telemetry frontend", + "main": "build/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "tsc && node build/index.js", + "tsc": "tsc" + }, "dependencies": { "@types/express": "^4.16.0", "@types/node": "^10.3.3", diff --git a/backend/tsconfig.json b/backend/tsconfig.json index f9d94c8..245bee7 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es2017", "module": "commonjs", - "outDir": "dist", + "outDir": "build", "strictNullChecks": true, "sourceMap": true, "moduleResolution": "node", diff --git a/frontend b/frontend new file mode 160000 index 0000000..f4c58d0 --- /dev/null +++ b/frontend @@ -0,0 +1 @@ +Subproject commit f4c58d01a72bae6827941582e21bb293b66040a4 diff --git a/shared/id.ts b/shared/id.ts new file mode 100644 index 0000000..69f44ea --- /dev/null +++ b/shared/id.ts @@ -0,0 +1,35 @@ +import { Opaque } from './types'; + +/** + * Unique type-constrained Id number. + */ +export type Id = Opaque; + +/** + * Higher order function producing new auto-incremented `Id`s. + */ +export function idGenerator(): () => Id { + let current = 0; + + return () => current++ as Id; +} + +interface HasId { + id: Id; +} + +export class IdSet { + private map: Map, T> = new Map(); + + public add(item: T & HasId) { + this.map.set(item.id, item); + } + + public remove(item: T & HasId) { + this.map.delete(item.id); + } + + public get entries(): IterableIterator { + return this.map.values(); + } +} diff --git a/shared/index.ts b/shared/index.ts index 3933796..6af35f0 100644 --- a/shared/index.ts +++ b/shared/index.ts @@ -1,109 +1,3 @@ -/** - * PhantomData akin to Rust, because sometimes you need to be smarter than - * the compiler. - */ -export class PhantomData

{ 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`. - * - * `P` can be anything as it's never actually used. Using strings is okay: - * - * ``` - * type MyType = Opaque; - * ``` - */ -export type Opaque = T & PhantomData

; - -/** - * Just a readable shorthand for null-ish-able types, akin to `T?` in Flow. - */ -export type Maybe = T | null | undefined; - -/** - * Higher order function producing new auto-incremented `Id`s. - */ -export function idGenerator(): () => Id { - let current = 0; - - return () => current++ as Id; -} - -/** - * Unique type-constrained Id number. - */ -export type Id = Opaque; - -interface HasId { - id: Id; -} - -export class IdSet { - private map: Map, T> = new Map(); - - public add(item: T & HasId) { - this.map.set(item.id, item); - } - - public remove(item: T & HasId) { - this.map.delete(item.id); - } - - public get entries(): IterableIterator { - return this.map.values(); - } -} - -export function* map(iter: IterableIterator, fn: (item: T) => U): IterableIterator { - for (const item of iter) yield fn(item); -} - -export function* chain(a: IterableIterator, b: IterableIterator): IterableIterator { - yield* a; - yield* b; -} - -export function* zip(a: IterableIterator, b: IterableIterator): 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(iter: IterableIterator, n: number): IterableIterator { - for (const item of iter) { - if (n-- === 0) return; - - yield item; - } -} - -export function skip(iter: IterableIterator, n: number): IterableIterator { - while (n-- !== 0 && !iter.next().done) {} - - return iter; -} - -export function reduce(iter: IterableIterator, 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; -} +export * from './id'; +export * from './iterators'; +export * from './types'; diff --git a/shared/iterators.ts b/shared/iterators.ts new file mode 100644 index 0000000..f0149dc --- /dev/null +++ b/shared/iterators.ts @@ -0,0 +1,62 @@ +export function* map(iter: IterableIterator, fn: (item: T) => U): IterableIterator { + for (const item of iter) { + yield fn(item); + } +} + +export function* chain(a: IterableIterator, b: IterableIterator): IterableIterator { + yield* a; + yield* b; +} + +export function* zip(a: IterableIterator, b: IterableIterator): 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(iter: IterableIterator, n: number): IterableIterator { + for (const item of iter) { + if (n-- === 0) { + return; + } + + yield item; + } +} + +export function skip(iter: IterableIterator, n: number): IterableIterator { + while (n-- !== 0 && !iter.next().done) {} + + return iter; +} + +export function reduce(iter: IterableIterator, 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; +} diff --git a/shared/types.ts b/shared/types.ts new file mode 100644 index 0000000..458cd80 --- /dev/null +++ b/shared/types.ts @@ -0,0 +1,22 @@ +/** + * PhantomData akin to Rust, because sometimes you need to be smarter than + * the compiler. + */ +export class PhantomData

{ 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`. + * + * `P` can be anything as it's never actually used. Using strings is okay: + * + * ``` + * type MyType = Opaque; + * ``` + */ +export type Opaque = T & PhantomData

; + +/** + * Just a readable shorthand for null-ish-able types, akin to `T?` in Flow. + */ +export type Maybe = T | null | undefined;