This file contains 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
class PairWeakMap<A extends object, B extends object, C> { | |
map: WeakMap<A, WeakMap<B, C>> | |
constructor() { | |
this.map = new WeakMap() | |
} | |
set(a: A, b: B, c: C): this { | |
const { map } = this | |
let submap = map.get(a) |
This file contains 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
interface Settable<A, B> { | |
set(key: A, value: B): this | |
} | |
interface Gettable<A, B> { | |
get(key: A): B | undefined | |
} | |
interface GenericMap<A, B> extends GetSettable<A, B> { | |
has(key: A): boolean |
This file contains 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 arraylikeToIterable = <A>(source: ArrayLike<A>): IterableIterator<A> => { | |
let i = 0 | |
return { | |
next() { | |
let done | |
let value | |
if (i < source.length) { | |
done = false | |
value = source[i] | |
i += 1 |
This file contains 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
class Deferred<A> { | |
promise: Promise<A> | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolve = resolve | |
this.reject = reject | |
}) | |
} | |
} | |
interface Deferred<A> { |
This file contains 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
class Deferred<A> { | |
promise: Promise<A> | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolve = resolve | |
this.reject = reject | |
}) | |
} | |
} |
This file contains 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 getState = memoize(<A>(o: AsyncQueue<A>) => { | |
const resolvers: Set<Array<(a: A) => void>> = new Set() | |
const values: A[] = [] | |
return { | |
resolvers, | |
values, | |
update() { | |
if (values.length === 0) { | |
return | |
} |
This file contains 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 Box<A> = () => A | null; | |
type Sealer<A> = { | |
seal(value: A): Box<A>; | |
unseal(box: Box<A>): A | null; | |
} | |
const makeSealer = <A>(): Sealer<A> => { | |
let open: Box<A> | null = null; | |
return { | |
seal(value: A): Box<A> { | |
const box = (): A | null => (open === box ? value : null); |
This file contains 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 prime = <A>(iterable: AsyncIterable<A>): AsyncIterator<A> => { | |
const iterator = iterable[Symbol.asyncIterator]() | |
iterator.next() | |
return iterator | |
} |
This file contains 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 _new = (constructor) => (...args) => constructor.apply(Object.create(constructor.prototype), args) |
This file contains 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 abortError = new Error(`Aborted`) | |
const promisifySignal = signal => | |
signal.aborted | |
? Promise.reject(abortError) | |
: new Promise( | |
(_, reject) => | |
void signal.addEventListener(`abort`, () => void reject(abortError), { once: true }), | |
) | |
const sleep = (delay, ...args) => new Promise(resolve => setTimeout(resolve, delay, ...args)) | |
const cancelableSleep = (delay, signal, ...args) => |