/** * connect react components with wire * * @packageDocumentation */ import { DependencyList } from 'react'; import { Dispatch } from 'react'; import { SetStateAction } from 'react'; declare type CB = (t: T) => void; declare type CovarianceGuard = [T, unknown]; export declare function createSelector( options: WritableSelectorOptions, ): Wire; export declare function createSelector( options: ReadOnlySelectorOptions, ): ReadonlyWire; export declare function createWire( initialValue: V, ): Wire; export declare type Defined = T extends undefined ? never : T; declare type Fn = (...args: any) => any; export declare interface FnsWire extends FnsWireGuard { fn: >(name: K, value: Fns[K]) => () => void; fns: Methods; } declare interface FnsWireGuard { /** * @internal * Covariance hack: * never use this variable. * don't remove space at the start */ ' fns-wire': StrictMethodsGuard; } export declare function getLinkIds(wire: undefined): undefined; export declare function getLinkIds(wire: WireId): LinkIds; export declare function getLinkIds( wire: WireId | undefined, ): LinkIds | undefined; export declare function getWireId(wire: undefined): undefined; export declare function getWireId(wire: WireId): string; export declare function getWireId(wire: WireId | undefined): string | undefined; declare type GetWireValue = (wire: ReadonlyStateWire) => V; declare type InitializerOrValue = V | (() => V); export declare type Interceptor = ( nextValue: Defined, preValue: Value, ) => Value | undefined; /** * isDefined check if the value is undefined or not. * it helps with typescript generic types. * @param value - */ export declare const isDefined: ( value: V | undefined, ) => value is Defined; declare type IsNever = [T] extends [never] ? true : false; declare type KeyOfMethods = NonNever>; export declare type LinkIds = Array; declare type MethodKeys = { [P in keyof T]: T[P] extends Fn ? P : never; }[keyof T]; declare type Methods = Pick>; declare type NonNever = IsNever extends true ? any : T; export declare interface ReadOnlySelectorOptions { get: (options: { get: GetWireValue }) => V; } export declare interface ReadonlyStateWire extends ReadonlyStateWireGuard, WireId { /** * get current value * @returns current value */ getValue(): V; /** * subscribe for value change * @param callback - * @returns unsubscribe function */ subscribe(callback: (value: Defined) => void): () => void; } declare interface ReadonlyStateWireGuard { ' state-wire': CovarianceGuard; } export declare type ReadonlyWire< V, Fns extends {} = {}, > = ReadonlyStateWire & FnsWire; declare type SetWireValue = (wire: StateWire, value: Defined) => void; export declare interface StateWire extends StateWireGuard, WireId { /** * get current value * @returns current value */ getValue(): V; /** * set value * @param value - */ setValue(value: Defined): void; /** * subscribe for value change * @param callback - * @returns unsubscribe function */ subscribe(callback: (value: Defined) => void): () => void; } declare interface StateWireGuard { ' state-wire': StrictGuard; } declare type StrictGuard = [T, CB]; declare type StrictMethodsGuard = { [P in keyof Methods]: StrictGuard; }; /** * * subscribe for function calls * * @param wire - * @param name - name of `fns` function * @param fn - memoized callback function * * @example ```ts // subscribe for `sample` function call useFn( wire, 'sample', useCallback(value => { console.log(value); }, []), ); // call `sample` function wire.fns.sample(5); ``` */ export declare function useFn>( wire: FnsWire | null | undefined, name: K, fn: Fns[K], ): void; /** * returns new wire and intercepting setValue of returned wire * * @param wire - up-link wire * @param interceptor - interceptor function * @returns new intercepted wire * * @remarks * * the interceptor function gets the next value and previous value and returns a value. the returned value be set on * up-link wire. * if interceptor function returns `undefined` or the same value as previous value, up-link wire value will not change. * * @example * * ```tsx * const valueWire = useInterceptor( * props.valueWire, * useCallback( * (nextValue, preValue) => (props.submittingWire.getValue() ? preValue : nextValue), * [props.submittingWire] * ) * ); * ``` * */ export declare function useInterceptor>( wire: W, interceptor: Interceptor>, ): W; export declare function useSelector( options: WritableSelectorOptions, deps?: DependencyList, ): Wire; export declare function useSelector( options: ReadOnlySelectorOptions, deps?: DependencyList, ): ReadonlyWire; export declare function useSubscribe( wire: ReadonlyStateWire, callback: (value: Defined) => void, ): void; export declare function useWire( upLink: Wire, ): Wire; export declare function useWire( upLink: Wire | null | undefined, initialValue: InitializerOrValue, ): Wire; export declare function useWire( upLink: Wire | null | undefined, initialValue: InitializerOrValue, ): Wire; export declare function useWire( upLink: Wire | null | undefined, initialValue?: InitializerOrValue, ): Wire; export declare function useWireState( upLink: StateWire, ): valueAndAction; export declare function useWireState( wire: StateWire | null | undefined, initialValue: InitializerOrValue, ): valueAndAction; export declare function useWireState( wire: StateWire | null | undefined, initialValue: InitializerOrValue, ): valueAndAction; export declare function useWireState( wire: StateWire | null | undefined, initialValue?: InitializerOrValue, ): valueAndAction; export declare function useWireValue( wire: null | undefined, defaultValue?: unknown, ): undefined; export declare function useWireValue>( wire: W, ): WireState; export declare function useWireValue>( wire: W | null | undefined, defaultValue: Defined>, ): Defined>; export declare function useWireValue>( wire: W, defaultValue?: WireState | undefined, ): WireState; export declare function useWireValue>( wire: W | null | undefined, defaultValue?: WireState, ): WireState | undefined; declare type valueAndAction = [V, Dispatch>>]; export declare type Wire = StateWire & FnsWire; export declare type WireFns> = W extends FnsWire ? Fns : never; export declare interface WireId { /** * @internal */ _getId(): string; /** * @internal */ _getLinkIds(): LinkIds; } export declare type WireState> = W extends ReadonlyStateWire ? V : never; export declare interface WritableSelectorOptions { get: (options: { get: GetWireValue }) => V; set: ( options: { get: GetWireValue; set: SetWireValue; }, value: V, ) => void; } export {};