/** * 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 * @flow strict-local * @format */ 'use strict'; import type { Loadable } from '../adt/Recoil_Loadable'; import type { TransactionInterface } from '../core/Recoil_AtomicUpdates'; import type { DefaultValue, PersistenceType } from '../core/Recoil_Node'; import type { RecoilState, RecoilValue } from '../core/Recoil_RecoilValue'; import type { ComponentSubscription } from '../core/Recoil_RecoilValueInterface'; import type { NodeKey, Store, TreeState } from '../core/Recoil_State'; const { atomicUpdater } = require('../core/Recoil_AtomicUpdates'); const { batchUpdates } = require('../core/Recoil_Batching'); const { DEFAULT_VALUE, getNode, nodes } = require('../core/Recoil_Node'); const { useRecoilMutableSource, useStoreRef } = require('../core/Recoil_RecoilRoot.react'); const { isRecoilValue } = require('../core/Recoil_RecoilValue'); const { AbstractRecoilValue, getRecoilValueAsLoadable, setRecoilValue, setRecoilValueLoadable, setUnvalidatedRecoilValue, subscribeToRecoilValue } = require('../core/Recoil_RecoilValueInterface'); const { updateRetainCount } = require('../core/Recoil_Retention'); const { RetentionZone } = require('../core/Recoil_RetentionZone'); const { Snapshot, cloneSnapshot } = require('../core/Recoil_Snapshot'); const { setByAddingToSet } = require('../util/Recoil_CopyOnWrite'); const differenceSets = require('../util/Recoil_differenceSets'); const { isSSR } = require('../util/Recoil_Environment'); const expectationViolation = require('../util/Recoil_expectationViolation'); const filterMap = require('../util/Recoil_filterMap'); const filterSet = require('../util/Recoil_filterSet'); const gkx = require('../util/Recoil_gkx'); const invariant = require('../util/Recoil_invariant'); const mapMap = require('../util/Recoil_mapMap'); const mergeMaps = require('../util/Recoil_mergeMaps'); const { mutableSourceExists, useMutableSource } = require('../util/Recoil_mutableSource'); const nullthrows = require('../util/Recoil_nullthrows'); const recoverableViolation = require('../util/Recoil_recoverableViolation'); const shallowArrayEqual = require('../util/Recoil_shallowArrayEqual'); const useComponentName = require('../util/Recoil_useComponentName'); const { useCallback, useEffect, useMemo, useRef, useState } = require('react'); // Components that aren't mounted after suspending for this long will be assumed // to be discarded and their resources released. const SUSPENSE_TIMEOUT_MS = 120000; declare function handleLoadable(loadable: Loadable, recoilValue: RecoilValue, storeRef: any): T; declare function validateRecoilValue(recoilValue: any, hookName: any): any; export type SetterOrUpdater = (((T) => T) | T) => void; export type Resetter = () => void; export type RecoilInterface = { getRecoilValue: (RecoilValue) => T, getRecoilValueLoadable: (RecoilValue) => Loadable, getRecoilState: (RecoilState) => [T, SetterOrUpdater], getRecoilStateLoadable: (RecoilState) => [Loadable, SetterOrUpdater], getSetRecoilState: (RecoilState) => SetterOrUpdater, getResetRecoilState: (RecoilState) => Resetter, }; /** * Various things are broken with useRecoilInterface, particularly concurrent mode * and memory management. They will not be fixed. * */ declare function useRecoilInterface_DEPRECATED(): RecoilInterface; const recoilComponentGetRecoilValueCount_FOR_TESTING = { current: 0 }; declare function useRecoilValueLoadable_MUTABLESOURCE(recoilValue: RecoilValue): Loadable; declare function useRecoilValueLoadable_LEGACY(recoilValue: RecoilValue): Loadable; /** Like useRecoilValue(), but either returns the value if available or just undefined if not available for any reason, such as pending or error. */ declare function useRecoilValueLoadable(recoilValue: RecoilValue): Loadable; /** Returns the value represented by the RecoilValue. If the value is pending, it will throw a Promise to suspend the component, if the value is an error it will throw it for the nearest React error boundary. This will also subscribe the component for any updates in the value. */ declare function useRecoilValue(recoilValue: RecoilValue): T; /** Returns a function that allows the value of a RecoilState to be updated, but does not subscribe the component to changes to that RecoilState. */ declare function useSetRecoilState(recoilState: RecoilState): SetterOrUpdater; /** Returns a function that will reset the value of a RecoilState to its default */ declare function useResetRecoilState(recoilState: RecoilState): Resetter; /** Equivalent to useState(). Allows the value of the RecoilState to be read and written. Subsequent updates to the RecoilState will cause the component to re-render. If the RecoilState is pending, this will suspend the component and initiate the retrieval of the value. If evaluating the RecoilState resulted in an error, this will throw the error so that the nearest React error boundary can catch it. */ declare function useRecoilState(recoilState: RecoilState): [T, SetterOrUpdater]; /** Like useRecoilState(), but does not cause Suspense or React error handling. Returns an object that indicates whether the RecoilState is available, pending, or unavailable due to an error. */ declare function useRecoilStateLoadable(recoilState: RecoilState): [Loadable, SetterOrUpdater]; declare function useTransactionSubscription(callback: (Store) => void): any; declare function externallyVisibleAtomValuesInState(state: TreeState): Map; type ExternallyVisibleAtomInfo = { persistence_UNSTABLE: { type: PersistenceType, backButton: boolean, ... }, ... }; /** Calls the given callback after any atoms have been modified and the consequent component re-renders have been committed. This is intended for persisting the values of the atoms to storage. The stored values can then be restored using the useSetUnvalidatedAtomValues hook. The callback receives the following info: atomValues: The current value of every atom that is both persistable (persistence type not set to 'none') and whose value is available (not in an error or loading state). previousAtomValues: The value of every persistable and available atom before the transaction began. atomInfo: A map containing the persistence settings for each atom. Every key that exists in atomValues will also exist in atomInfo. modifiedAtoms: The set of atoms that were written to during the transaction. transactionMetadata: Arbitrary information that was added via the useSetUnvalidatedAtomValues hook. Useful for ignoring the useSetUnvalidatedAtomValues transaction, to avoid loops. */ declare function useTransactionObservation_DEPRECATED(callback: ({ atomValues: Map, previousAtomValues: Map, atomInfo: Map, modifiedAtoms: $ReadOnlySet, transactionMetadata: { [NodeKey]: mixed, ... }, }) => void): any; declare function useRecoilTransactionObserver(callback: ({ snapshot: Snapshot, previousSnapshot: Snapshot, }) => void): any; declare function usePrevious(value: T): T | void; // Return a snapshot of the current state and subscribe to all state changes declare function useRecoilSnapshot(): Snapshot; declare function useGotoRecoilSnapshot(): (Snapshot) => void; declare function useSetUnvalidatedAtomValues(): (values: Map, transactionMetadata?: {...}) => void; export type RecoilCallbackInterface = $ReadOnly<{ set: (RecoilState, ((T) => T) | T) => void, reset: (RecoilState) => void, snapshot: Snapshot, gotoSnapshot: (Snapshot) => void, transact_UNSTABLE: ((TransactionInterface) => void) => void, }>; declare class Sentinel {} const SENTINEL = new Sentinel(); declare function useRecoilCallback, Return>(fn: (RecoilCallbackInterface) => (...Args) => Return, deps?: $ReadOnlyArray): (...Args) => Return; // I don't see a way to avoid the any type here because we want to accept readable // and writable values with any type parameter, but normally with writable ones // RecoilState is not a subtype of RecoilState. type ToRetain = RecoilValue // flowlint-line unclear-type:off | RetentionZone | $ReadOnlyArray | RetentionZone>; // flowlint-line unclear-type:off declare function useRetain(toRetain: ToRetain): void; declare function useRetain_ACTUAL(toRetain: ToRetain): void; declare function useRecoilTransaction>(fn: (TransactionInterface) => (...Arguments) => void, deps?: $ReadOnlyArray): (...Arguments) => void; module.exports = { recoilComponentGetRecoilValueCount_FOR_TESTING, useGotoRecoilSnapshot, useRecoilCallback, useRecoilInterface: useRecoilInterface_DEPRECATED, useRecoilSnapshot, useRecoilState, useRecoilStateLoadable, useRecoilTransaction, useRecoilTransactionObserver, useRecoilValue, useRecoilValueLoadable, useRetain, useResetRecoilState, useSetRecoilState, useSetUnvalidatedAtomValues, useTransactionObservation_DEPRECATED, useTransactionSubscription_DEPRECATED: useTransactionSubscription };