This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type _Boolean = <A>(onTrue: () => A, onFalse: () => A) => A | |
| const _true: _Boolean = (onTrue) => onTrue() | |
| const _false: _Boolean = (_, onFalse) => onFalse() | |
| const _if = <A>(condition: _Boolean, onTrue: () => A, onFalse: () => A): A => { | |
| return condition(onTrue, onFalse) | |
| } | |
| const _and = (a: _Boolean,b: _Boolean): _Boolean => { | |
| return (onTrue: () => any, onFalse: () => any) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const parMap = async <T, U>(array:T[], callback: (value: T) => U | PromiseLike<U>): Promise<U[]> => { | |
| return Promise.all(array.map(callback)) | |
| } | |
| const parEach = async <T, U>(array:T[], callback: (value: T) => void | PromiseLike<void>): Promise<void> => { | |
| await Promise.all(array.map(callback)) | |
| } | |
| async function main() { | |
| const names = ["foo.txt","baz.txt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type PVar<T> = Promise<T> & { | |
| resolve: (res: T | PromiseLike<T>) => void; | |
| reject: (reason?: unknown) => void; | |
| }; | |
| function mkPVar<T>() { | |
| const result: PVar<T> = new Promise<T>((resolve, reject) => { | |
| result.resolve = resolve; | |
| result.reject = reject; | |
| }) as PVar<T>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface Canceler { | |
| (): void; | |
| } | |
| /** No operation canceler which "does nothing". | |
| * | |
| * ```js | |
| * concatCancelers(a, emptyCanceler) ==== concatCancelers(emptyCanceler, a) ==== a | |
| * ``` | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Stricter and Safer version of Type Guards. | |
| * | |
| * It takes function that actually returns Output type and constructs | |
| * type guard which is actually safe and can't ever go out of sync. | |
| * for example this type guard compiles: | |
| * ```ts | |
| * function isNumber(x: any): x is number { | |
| * return typeof x === "string"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type CleanUp = () => void | |
| export class Mutex<T> { | |
| private mutex:Promise<void> = Promise.resolve(); | |
| lock(): Promise<CleanUp> { | |
| let resolver = (unlock: CleanUp): void => {}; | |
| const result = new Promise<CleanUp>(resolve => { resolver = resolve }); | |
| // whatever is running currently, let's wait untill that's done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Decoder<I,O> = (_:I) => O | |
| type TypeOf<D> = D extends Decoder<any, infer R> ? R: never | |
| type InputOf<D> = D extends Decoder<infer R, any> ? R: never | |
| const string: Decoder<unknown, string> = (x) => { | |
| if (typeof x === "string") return x; | |
| throw new Error("Expected string"); | |
| } | |
| const number: Decoder<unknown, number> = (x) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const consoleLogger = (...args: unknown[]) => { | |
| console.log(...args); | |
| }; | |
| const consoleTracer = (...args: unknown[]) => { | |
| console.trace(...args); | |
| }; | |
| type MonitorAction = "get" | "set"; | |
| type MonitorDesc<T> = [prop: keyof T, ...actions: MonitorAction[]]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Listener<T> = [T] extends [void] ? () => void : (payload: T) => void; | |
| type BaseSpec = Record<string, unknown>; | |
| type ListenerMap<T extends BaseSpec> = { [K in keyof T]?: Listener<T[K]>[] }; | |
| type EventPayload<T> = [T] extends [void] ? [] : [payload: T]; | |
| /** Typed event emitter implementation | |
| * example: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type RPCResponsev1 = | |
| { error?: string | |
| , result?: string | |
| } | |
| // TS will not let this compile as it know there might be case | |
| // where both error and result is null and function will just return implicitly with undefined. | |
| function example(res:RPCResponsev1): number { | |
| if(res.error == null) { | |
| return 1 |