// Minimum TypeScript Version: 3.7 /** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @emails oncall+recoil */ /** * This file is a manual translation of the flow types, which are the source of truth, so we should not introduce new terminology or behavior in this file. */ export { }; import * as React from 'react'; // state.d.ts type NodeKey = string; // node.d.ts export class DefaultValue { private __tag: 'DefaultValue'; } // recoilRoot.d.ts export type RecoilRootProps = { initializeState?: (mutableSnapshot: MutableSnapshot) => void, override?: true, } | {override: false}; /** * Root component for managing Recoil state. Most Recoil hooks should be * called from a component nested in a */ export const RecoilRoot: React.FC; // Snapshot.d.ts declare const SnapshotID_OPAQUE: unique symbol; export interface SnapshotID { readonly [SnapshotID_OPAQUE]: true; } interface ComponentInfo { name: string; } interface RecoilStateInfo { loadable?: Loadable; isActive: boolean; isSet: boolean; isModified: boolean; // TODO report modified selectors type: 'atom' | 'selector' | undefined; // undefined until initialized for now deps: Iterable>; subscribers: { nodes: Iterable>, components: Iterable, }; } export class Snapshot { getID(): SnapshotID; getLoadable(recoilValue: RecoilValue): Loadable; getPromise(recoilValue: RecoilValue): Promise; getNodes_UNSTABLE(opts?: { isModified?: boolean, isInitialized?: boolean }): Iterable>; getInfo_UNSTABLE(recoilValue: RecoilValue): RecoilStateInfo; map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot; asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise): Promise; retain(): () => void; } export class MutableSnapshot extends Snapshot { set: SetRecoilState; reset: ResetRecoilState; } // Effect is called the first time a node is used with a export type AtomEffect = (param: { node: RecoilState, trigger: 'set' | 'get', // Call synchronously to initialize value or async to change it later setSelf: (param: | T | DefaultValue | Promise | ((param: T | DefaultValue) => T | DefaultValue), ) => void, resetSelf: () => void, // Subscribe callbacks to events. // Atom effect observers are called before global transaction observers onSet: ( param: (newValue: T, oldValue: T | DefaultValue, isReset: boolean) => void, ) => void, // Accessors to read other atoms/selectors getPromise: (recoilValue: RecoilValue) => Promise, getLoadable: (recoilValue: RecoilValue) => Loadable, getInfo_UNSTABLE: (recoilValue: RecoilValue) => RecoilStateInfo, }) => void | (() => void); // atom.d.ts export interface AtomOptions { key: NodeKey; default: RecoilValue | Promise | T; effects_UNSTABLE?: ReadonlyArray>; dangerouslyAllowMutability?: boolean; } /** * Creates an atom, which represents a piece of writeable state */ export function atom(options: AtomOptions): RecoilState; // selector.d.ts export type GetRecoilValue = (recoilVal: RecoilValue) => T; export type GetCallback = , Return>( fn: (interface: Readonly<{snapshot: Snapshot}>) => (...args: Args) => Return, ) => (...args: Args) => Return; export type SetRecoilState = ( recoilVal: RecoilState, newVal: T | DefaultValue | ((prevValue: T) => T | DefaultValue), ) => void; export type ResetRecoilState = (recoilVal: RecoilState) => void; // eslint-disable-line @typescript-eslint/no-explicit-any // export type EqualityPolicy = 'reference' | 'value'; TODO: removing while we discuss long term API export type EvictionPolicy = 'lru' | 'keep-all' | 'most-recent'; // TODO: removing while we discuss long term API // export type CachePolicy = // | {eviction: 'lru', maxSize: number, equality?: EqualityPolicy} // | {eviction: 'none', equality?: EqualityPolicy} // | {eviction?: undefined, equality: EqualityPolicy}; // TODO: removing while we discuss long term API // export interface CachePolicyWithoutEviction { // equality: EqualityPolicy; // } export type CachePolicyWithoutEquality = {eviction: 'lru', maxSize: number} | {eviction: 'keep-all'} | {eviction: 'most-recent'}; export interface ReadOnlySelectorOptions { key: string; get: (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; dangerouslyAllowMutability?: boolean; cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API } export interface ReadWriteSelectorOptions extends ReadOnlySelectorOptions { set: ( opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState; }, newValue: T | DefaultValue, ) => void; } /** * Creates a selector which represents derived state. */ export function selector(options: ReadWriteSelectorOptions): RecoilState; export function selector(options: ReadOnlySelectorOptions): RecoilValueReadOnly; // hooks.d.ts export type SetterOrUpdater = (valOrUpdater: ((currVal: T) => T) | T) => void; export type Resetter = () => void; export interface TransactionInterface_UNSTABLE { get(a: RecoilValue): T; set(s: RecoilState, u: ((currVal: T) => T) | T): void; reset(s: RecoilState): void; } export type CallbackInterface = Readonly<{ set: (recoilVal: RecoilState, valOrUpdater: ((currVal: T) => T) | T) => void; reset: (recoilVal: RecoilState) => void; // eslint-disable-line @typescript-eslint/no-explicit-any snapshot: Snapshot, gotoSnapshot: (snapshot: Snapshot) => void, transact_UNSTABLE: (cb: (i: TransactionInterface_UNSTABLE) => void) => void; }>; /** * Returns the value of an atom or selector (readonly or writeable) and * subscribes the components to future updates of that state. */ export function useRecoilValue(recoilValue: RecoilValue): T; /** * Returns a Loadable representing the status of the given Recoil state * and subscribes the component to future updates of that state. Useful * for working with async selectors. */ export function useRecoilValueLoadable(recoilValue: RecoilValue): Loadable; /** * Returns a tuple where the first element is the value of the recoil state * and the second is a setter to update that state. Subscribes component * to updates of the given state. */ export function useRecoilState(recoilState: RecoilState): [T, SetterOrUpdater]; /** * Returns a tuple where the first element is a Loadable and the second * element is a setter function to update the given state. Subscribes * component to updates of the given state. */ export function useRecoilStateLoadable(recoilState: RecoilState): [Loadable, SetterOrUpdater]; /** * Returns a setter function for updating Recoil state. Does not subscribe * the component to the given state. */ export function useSetRecoilState(recoilState: RecoilState): SetterOrUpdater; /** * Returns a function that will reset the given state to its default value. */ export function useResetRecoilState(recoilState: RecoilState): Resetter; // eslint-disable-line @typescript-eslint/no-explicit-any /** * Returns current info about an atom */ export function useGetRecoilValueInfo_UNSTABLE(): (recoilValue: RecoilValue) => RecoilStateInfo; /** * Returns a function that will run the callback that was passed when * calling this hook. Useful for accessing Recoil state in response to * events. */ export function useRecoilCallback, Return>( fn: (interface: CallbackInterface) => (...args: Args) => Return, deps?: ReadonlyArray, ): (...args: Args) => Return; /** * Returns a function that executes an atomic transaction for updating Recoil state. */ export function useRecoilTransaction_UNSTABLE>( fn: (interface: TransactionInterface_UNSTABLE) => (...args: Args) => void, deps?: ReadonlyArray, ): (...args: Args) => void; export function useRecoilTransactionObserver_UNSTABLE( callback: (opts: { snapshot: Snapshot, previousSnapshot: Snapshot, }) => void, ): void; /** * Updates Recoil state to match the provided snapshot. */ export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void; /** * Returns a snapshot of the current Recoil state and subscribes the component * to re-render when any state is updated. */ export function useRecoilSnapshot(): Snapshot; // useRecoilRefresher.d.ts /** * Clears the cache for a selector causing it to be reevaluated. */ export function useRecoilRefresher_UNSTABLE(recoilValue: RecoilValue): () => void; // useRecoilBridgeAcrossReactRoots.d.ts export const RecoilBridge: React.FC; /** * Returns a component that acts like a but shares the same store * as the current . */ export function useRecoilBridgeAcrossReactRoots_UNSTABLE(): typeof RecoilBridge; // loadable.d.ts interface BaseLoadable { getValue: () => T; toPromise: () => Promise; valueOrThrow: () => T; errorOrThrow: () => any; promiseOrThrow: () => Promise; is: (other: Loadable) => boolean; map: (map: (from: T) => Loadable | Promise | S) => Loadable; } interface ValueLoadable extends BaseLoadable { state: 'hasValue'; contents: T; valueMaybe: () => T; errorMaybe: () => undefined; promiseMaybe: () => undefined; } interface LoadingLoadable extends BaseLoadable { state: 'loading'; contents: Promise; valueMaybe: () => undefined; errorMaybe: () => undefined; promiseMaybe: () => Promise; } interface ErrorLoadable extends BaseLoadable { state: 'hasError'; contents: any; valueMaybe: () => undefined; errorMaybe: () => any; promiseMaybe: () => undefined; } export type Loadable = | ValueLoadable | LoadingLoadable | ErrorLoadable; // recoilValue.d.ts declare class AbstractRecoilValue { __tag: [T]; __cTag: (t: T) => void; // for contravariance key: NodeKey; constructor(newKey: NodeKey); } declare class AbstractRecoilValueReadonly { __tag: [T]; key: NodeKey; constructor(newKey: NodeKey); } export class RecoilState extends AbstractRecoilValue {} export class RecoilValueReadOnly extends AbstractRecoilValueReadonly {} export type RecoilValue = RecoilValueReadOnly | RecoilState; /** * Returns true if the parameter is a Recoil atom or selector. */ export function isRecoilValue(val: unknown): val is RecoilValue; // eslint-disable-line @typescript-eslint/no-explicit-any /** Utilities */ // bigint not supported yet type Primitive = undefined | null | boolean | number | symbol | string; export type SerializableParam = | Primitive | {toJSON: () => string} | ReadonlyArray | Readonly<{[key: string]: SerializableParam}>; export interface AtomFamilyOptions { key: NodeKey; dangerouslyAllowMutability?: boolean; default: RecoilValue | Promise | T | ((param: P) => T | RecoilValue | Promise); effects_UNSTABLE?: | ReadonlyArray> | ((param: P) => ReadonlyArray>); // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API } /** * Returns a function which returns a memoized atom for each unique parameter value. */ export function atomFamily( options: AtomFamilyOptions, ): (param: P) => RecoilState; export interface ReadOnlySelectorFamilyOptions { key: string; get: (param: P) => (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API dangerouslyAllowMutability?: boolean; } export interface ReadWriteSelectorFamilyOptions { key: string; get: (param: P) => (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; set: ( param: P, ) => ( opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState }, newValue: T | DefaultValue, ) => void; // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API dangerouslyAllowMutability?: boolean; } /** * Returns a function which returns a memoized atom for each unique parameter value. */ export function selectorFamily( options: ReadWriteSelectorFamilyOptions, ): (param: P) => RecoilState; /** * Returns a function which returns a memoized atom for each unique parameter value. */ export function selectorFamily( options: ReadOnlySelectorFamilyOptions, ): (param: P) => RecoilValueReadOnly; /** * Returns a selector that always has a constant value. */ export function constSelector(constant: T): RecoilValueReadOnly; /** * Returns a selector which is always in the provided error state. */ export function errorSelector(message: string): RecoilValueReadOnly; /** * Casts a selector to be a read-only selector */ export function readOnlySelector(atom: RecoilValue): RecoilValueReadOnly; /** * Returns a selector that has the value of the provided atom or selector as a Loadable. * This means you can use noWait() to avoid entering an error or suspense state in * order to manually handle those cases. */ export function noWait(state: RecoilValue): RecoilValueReadOnly>; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UnwrapRecoilValue = T extends RecoilValue ? R : never; export type UnwrapRecoilValues> | { [key: string]: RecoilValue }> = { [P in keyof T]: UnwrapRecoilValue; }; export type UnwrapRecoilValueLoadables> | { [key: string]: RecoilValue }> = { [P in keyof T]: Loadable>; }; export function waitForNone> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForNone }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAny> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAny }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAll> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAll }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAllSettled> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAllSettled }>( param: RecoilValues, ): RecoilValueReadOnly>; export type UnwrapLoadable = T extends Loadable ? R : never; export type UnwrapLoadables> | { [key: string]: Loadable }> = { [P in keyof T]: UnwrapLoadable; }; /* eslint-disable @typescript-eslint/no-unused-vars */ export namespace RecoilLoadable { /** * Factory to make a Loadable object. If a Promise is provided the Loadable will * be in a 'loading' state until the Promise is either resolved or rejected. */ function of(x: T | Promise): Loadable; /** * Factory to make a Loadable object in an error state. */ function error(x: any): ErrorLoadable; /** * Factory to make a Loadable which is resolved when all of the Loadables provided * to it are resolved or any one has an error. The value is an array of the values * of all of the provided Loadables. This is comparable to Promise.all() for Loadables. */ function all> | [Loadable]>(inputs: Inputs): Loadable>; function all}>(inputs: Inputs): Loadable>; /** * Returns true if the provided parameter is a Loadable type. */ function isLoadable(x: any): x is Loadable; } /* eslint-enable @typescript-eslint/no-unused-vars */ /* eslint-enable @typescript-eslint/no-explicit-any */ /** * Factory to produce a Recoil snapshot object with all atoms in the default state. */ export function snapshot_UNSTABLE(initializeState?: (shapshot: MutableSnapshot) => void): Snapshot;