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
/* | |
let a = 1 | |
let b = 2 | |
const u = plus(a, mul(b,b)) | |
const f = x => plus(a, x) | |
const k = f(b) |
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 { any } from "prop-types"; | |
type Cell = { initialized: boolean; state: any; next: any }; | |
export function Cell(): Cell { | |
return { initialized: false, state: null, next: null }; | |
} | |
export type CellFunction = <State, Initial>( | |
callback: (state: State | Initial) => State, |
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 Lazy<Value> = () => Value | |
export function lazy<Value>(thunk: () => Value){ | |
let value: Value // will hold computed value | |
let factory: (() => Value) | null = thunk // used as check if value is computed and allow thunk to be garbage collected | |
return () => { | |
if (!factory) { | |
return value | |
} else { | |
value = factory() |
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 function AsyncNode({ children }: { children: Promise<ReactNode> }) { | |
return useLatestResolvedValue<ReactNode>(children, () => null) as any; | |
// as any assertion needed for issue https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20356#issuecomment-336384210 | |
} | |
export function useLatestResolvedValue<Value>( | |
promise: Promise<Value>, | |
initial: () => Value | |
) { | |
const [[value], dispatch] = useReducer< |
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 FiberFunction<Args extends any[], Return> = ( | |
cell: CellFunction | |
) => (...args: Args) => Return; | |
type FiberInstance<Args extends any[], Return> = ( | |
...args: Args | |
) => [Return, FiberInstance<Args, Return>]; | |
type FiberFactory = <Args extends any[], Return>( | |
fiber: FiberFunction<Args, Return>, |
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
function useUndoState<T>(initial: T) { | |
const [index, setIndex] = useState(0); | |
const [history, setHistory] = useState([initial]); | |
const state = history[index]; | |
const setState = useCallback( | |
(update: (s: T) => T) => { | |
const updated = history.slice(0, index + 1); | |
updated.push(update(state)); | |
setHistory(updated); | |
setIndex(index + 1); |
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
function useUndoReducer<State, Action>( | |
reducer: (state: State, action: Action) => State, | |
initial: () => State[] | |
) { | |
const [index, setIndex] = useState(0); | |
const [history, setHistory] = useState(initial); | |
const state = history[index]; | |
const dispatch = useCallback( | |
(action: Action) => { | |
const updated = history.slice(0, index + 1); |
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 React from "react"; | |
export default function useDebounce<T>(value: T, delay: number) { | |
const [debouncedValue, setDebouncedValue] = React.useState(value); | |
React.useEffect(() => { | |
const timeout = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); |
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 { useRef } from "react"; | |
/** | |
* @description used for identify changes between re-renders | |
* @example | |
* const changedProps = useChanges(props) | |
* @example | |
* useChanges(props, changes => console.log(changes)) | |
*/ | |
export function useDebugPropsChanges<T extends Record<any, 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
import { useState, useEffect } from "react"; | |
/** @description hook: returns true if given timestamp is in the future */ | |
export function useExpiration( | |
timestamp: number | null | undefined, | |
): boolean { | |
const visible = timestamp != null && Date.now() <= timestamp; | |
const [, forceRerender] = useState(0); | |
useEffect(() => { | |
if (visible && timestamp != null) { |