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
| import { EventObject, State, StateSchema, Typestate, createMachine, interpret } from 'xstate'; | |
| type MatchCase< | |
| TContext, | |
| TTypestate extends Typestate<TContext>, | |
| TSV extends TTypestate['value'], | |
| TResult | |
| > = readonly [...TSV[], TResult]; | |
| // TODO: |
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
| import { useMemo } from "react"; | |
| import { Interpreter } from "xstate"; | |
| import { useService } from "@xstate/react"; | |
| export function useAllowedEvents(service: Interpreter<any>) { | |
| const [state] = useService(service); | |
| const allowedEvents = useMemo( | |
| () => | |
| state.nextEvents.filter( | |
| (event) => service.machine.transition(state, event).changed |
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
| import { useMemo } from "react"; | |
| import { Interpreter } from "xstate"; | |
| import { useService } from "@xstate/react"; | |
| export function useAllowedEvents(service: Interpreter<any>) { | |
| const [state] = useService(service); | |
| const allowedEvents = useMemo( | |
| () => | |
| state.nextEvents.filter( | |
| (event) => service.machine.transition(state, event).changed |
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
| import { RefObject, useEffect, useState } from 'react'; | |
| export function useLineCount(ref: RefObject<HTMLElement>): number { | |
| const [lines, setLines] = useState(0); | |
| useEffect(() => { | |
| const resizeObserver = new ResizeObserver(entries => { | |
| entries.forEach(entry => { | |
| const elementHeight = (entry.target as HTMLElement).offsetHeight; | |
| const lineHeight = parseInt(getComputedStyle(entry.target).lineHeight, 10); |
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 type ResolvedResult<T> = [error: null, value: T]; | |
| export type RejectedResult = [error: unknown, value: null]; | |
| export type Result<T> = ResolvedResult<T> | RejectedResult; | |
| export function getResult<T>(promise: PromiseLike<T>): Promise<ResolvedResult<T> | RejectedResult> { | |
| return Promise.resolve(promise) | |
| .then(value => [null, value] as [null, T]) | |
| .catch(error => [error, null] as [unknown, null]); | |
| } |
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 IterableRange = IterableIterator<number> & { | |
| start: number; | |
| end: number; | |
| step: number; | |
| }; | |
| function range( | |
| startOrEnd: number, | |
| maybeEnd?: number, | |
| maybeStep?: number |
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
| class MapKeySetView<T> implements Set<T> { | |
| readonly #map: Map<T, unknown>; | |
| constructor(map: Map<T, unknown>) { | |
| this.#map = map; | |
| } | |
| get size() { | |
| return this.#map.size; | |
| } |
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
| # Revert test files that have one line modified | |
| git diff --numstat \ | |
| | grep 'test.tsx' \ | |
| | awk '{ if($1==1 && $2==1) print $3 }' \ | |
| | xargs git checkout -- |
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 const pick = < | |
| T extends Record<keyof unknown, unknown>, | |
| K extends keyof T | |
| >( | |
| object: T, | |
| paths: readonly K[], | |
| ): Pick<T, K> => | |
| Object.assign({}, ...paths.map((path) => ({ [path]: object[path] }))); |
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
| import { JsonValue } from "type-fest"; | |
| function toSerializable(value: unknown): JsonValue | undefined { | |
| switch (typeof value) { | |
| case "string": | |
| case "boolean": | |
| return value; | |
| case "number": | |
| return !isFinite(value) ? null : value; | |
| case "object": { |