recoil
Version:
Recoil - A state management library for React
111 lines (97 loc) • 4.55 kB
Flow
/**
* 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
*/
;
import type { Loadable } from '../adt/Recoil_Loadable';
import type { RecoilValue } from './Recoil_RecoilValue';
import type { RetainedBy } from './Recoil_RetainedBy';
import type { AtomWrites, NodeKey, Store, TreeState } from './Recoil_State';
const expectationViolation = require('../util/Recoil_expectationViolation');
const gkx = require('../util/Recoil_gkx');
const mapIterable = require('../util/Recoil_mapIterable');
const nullthrows = require('../util/Recoil_nullthrows');
const recoverableViolation = require('../util/Recoil_recoverableViolation');
const RecoilValueClasses = require('./Recoil_RecoilValue');
declare class DefaultValue {}
const DEFAULT_VALUE: DefaultValue = new DefaultValue();
declare class RecoilValueNotReady extends Error {
constructor(key: NodeKey): any
}
export type PersistenceType = 'none' | 'url';
export type PersistenceInfo = $ReadOnly<{
type: PersistenceType,
backButton?: boolean,
}>;
export type Trigger = 'get' | 'set';
type NodeType = 'atom' | 'selector';
export type ReadOnlyNodeOptions<T> = $ReadOnly<{
key: NodeKey,
nodeType: NodeType,
// Returns the current value without evaluating or modifying state
peek: (Store, TreeState) => ?Loadable<T>,
// Returns the discovered deps and the loadable value of the node
get: (Store, TreeState) => Loadable<T>,
// Informs the node the first time it is used (either ever or since the node was
// last released). Returns a cleanup function for when the store ceases to be or
// the node is released again.
init: (Store, TreeState, Trigger) => () => void,
// Invalidate the cached value stored in the TreeState.
// It is used at the end of each batch for mutated state.
// This does not affect any other caches such as the selector cache.
invalidate: (TreeState) => void,
// Clear all internal caches for this node. Unlike "invalidate()" this clears
// the selector cache and clears for all possible dependency values.
clearCache?: (Store, TreeState) => void,
shouldRestoreFromSnapshots: boolean,
dangerouslyAllowMutability?: boolean,
persistence_UNSTABLE?: PersistenceInfo,
// True for members of families, since another node can be created later for the
// same parameter value; but false for individual atoms and selectors which have
// a singleton config passed to us only once when they're defined:
shouldDeleteConfigOnRelease?: () => boolean,
retainedBy: RetainedBy,
}>;
export type ReadWriteNodeOptions<T> = $ReadOnly<{ ...ReadOnlyNodeOptions<T>,
// Returns the discovered deps and the set of key-value pairs to be written.
// (Deps may be discovered since selectors get an updater function which has
// the ability to read other atoms, which may have deps.)
set: (store: Store, state: TreeState, newValue: T | DefaultValue) => AtomWrites,
}>;
type Node<T> = ReadOnlyNodeOptions<T> | ReadWriteNodeOptions<T>; // flowlint-next-line unclear-type:off
const nodes: Map<string, Node<any>> = new Map(); // flowlint-next-line unclear-type:off
const recoilValues: Map<string, RecoilValue<any>> = new Map();
/* eslint-disable no-redeclare */
declare function registerNode<T>(node: ReadWriteNodeOptions<T>): RecoilValueClasses.RecoilState<T>;
declare function registerNode<T>(node: ReadOnlyNodeOptions<T>): RecoilValueClasses.RecoilValueReadOnly<T>;
declare function recoilValuesForKeys(keys: Iterable<NodeKey>): Iterable<RecoilValue<mixed>>;
declare function registerNode<T>(node: Node<T>): RecoilValue<T>;
/* eslint-enable no-redeclare */
declare class NodeMissingError extends Error {} // flowlint-next-line unclear-type:off
declare function getNode(key: NodeKey): Node<any>; // flowlint-next-line unclear-type:off
declare function getNodeMaybe(key: NodeKey): void | Node<any>;
const configDeletionHandlers = new Map();
declare function deleteNodeConfigIfPossible(key: NodeKey): void;
declare function setConfigDeletionHandler(key: NodeKey, fn: void | (() => void)): void;
declare function getConfigDeletionHandler(key: NodeKey): void | (() => void);
module.exports = {
nodes,
recoilValues,
registerNode,
getNode,
getNodeMaybe,
deleteNodeConfigIfPossible,
setConfigDeletionHandler,
getConfigDeletionHandler,
recoilValuesForKeys,
NodeMissingError,
DefaultValue,
DEFAULT_VALUE,
RecoilValueNotReady
};