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 Task<T> = () => Promise<T>; | |
export class TaskPool { | |
#isReady: boolean; | |
#current: Task<unknown> | null; | |
readonly #queue: Task<unknown>[]; | |
constructor( | |
params: { | |
readonly isReady?: 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
export class KeyPath<T extends string = string> { | |
#_brand!: never; | |
readonly #path: readonly string[]; | |
constructor(path: T) { | |
this.#path = path.split("."); | |
} | |
of<Expected>(object: unknown): Expected { |
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 ReadonlyMapConstructor { | |
new <K, V>(entries?: IterableIterator<[K, V]> | null): ReadonlyMap<K, V>; | |
} | |
class ReadonlyMapClass<K, V> extends Map<K, V> {} | |
export const ReadonlyMap: ReadonlyMapConstructor = ReadonlyMapClass; | |
// |
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 Action<T, U = unknown> { | |
act: (state: T) => Promise<U>; | |
deriveActual: (state: T, actResult: U) => T; | |
deriveOptimistic: (state: T) => T; | |
} | |
export class OptimisticStore<T> { | |
#optimisticState: T; | |
#committedState: T; | |
readonly #runner = new SerialTaskRunner(); |
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
declare const historyIdBrand: unique symbol; | |
let historyId = 0; | |
type HistoryId = number & { [historyIdBrand]: never }; | |
interface HistoryItem<T, A> { | |
readonly id: HistoryId; | |
readonly action: A; | |
readonly state: T; |
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
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
export const debounce = <T extends (...args: any[]) => void>( | |
fn: T, | |
timeout: number, | |
): T => { | |
let timeoutId: number | undefined; | |
return new Proxy(fn, { | |
apply: (target, thisArg, args) => { | |
window.clearInterval(timeoutId); |
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 DetailedResponse<Status extends number, Body> extends Response { | |
status: Status; | |
json: () => Promise<Body>; | |
} | |
interface DetailedRequest<ExpectedResponse extends Response> extends Request {} | |
type DetailedFetch = <Req extends DetailedRequest<Res>, Res extends Response = Req extends DetailedRequest<infer U> ? U : never>(request: Req, init?: RequestInit) => Promise<Res>; | |
const detailedFetch = fetch as DetailedFetch; |
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 Hash = string | number | symbol | object; | |
interface Hashable<K extends Hash> { | |
readonly hash: K; | |
} | |
class FooId implements Hashable<number> { | |
readonly primitive: number; | |
constructor(primitive: number) { |
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
export const uniqueArray = <E>( | |
array: readonly E[], | |
isEqual: (a: E, b: E) => boolean = Object.is | |
): E[] => { | |
return array.filter( | |
(a, index, self) => index == self.findIndex((b) => isEqual(a, b)) | |
); | |
}; |
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 {Map<string, Set<string>>} */ | |
const dependencyMap = /* provide source */; | |
/** @type {Set<string>} */ | |
const searched = new Set(); | |
/** | |
* @param {string} target | |
* @param {string[]} path | |
*/ |
NewerOlder