1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export { };
|
8 |
|
9 | import * as React from 'react';
|
10 |
|
11 |
|
12 | type NodeKey = string;
|
13 |
|
14 |
|
15 | export class DefaultValue {
|
16 | private __tag: 'DefaultValue';
|
17 | }
|
18 |
|
19 |
|
20 | export type RecoilRootProps = {
|
21 | initializeState?: (mutableSnapshot: MutableSnapshot) => void,
|
22 | override?: true,
|
23 | } | {override: false};
|
24 |
|
25 | export const RecoilRoot: React.FC<RecoilRootProps>;
|
26 |
|
27 |
|
28 | declare const SnapshotID_OPAQUE: unique symbol;
|
29 | export interface SnapshotID {
|
30 | readonly [SnapshotID_OPAQUE]: true;
|
31 | }
|
32 |
|
33 | interface ComponentInfo {
|
34 | name: string;
|
35 | }
|
36 |
|
37 | interface AtomInfo<T> {
|
38 | loadable?: Loadable<T>;
|
39 | isActive: boolean;
|
40 | isSet: boolean;
|
41 | isModified: boolean;
|
42 | type: 'atom' | 'selector' | undefined;
|
43 | deps: Iterable<RecoilValue<T>>;
|
44 | subscribers: {
|
45 | nodes: Iterable<RecoilValue<T>>,
|
46 | components: Iterable<ComponentInfo>,
|
47 | };
|
48 | }
|
49 |
|
50 | export class Snapshot {
|
51 | getID(): SnapshotID;
|
52 | getLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
|
53 | getPromise<T>(recoilValue: RecoilValue<T>): Promise<T>;
|
54 | getNodes_UNSTABLE(opts?: { isModified?: boolean, isInitialized?: boolean }): Iterable<RecoilValue<unknown>>;
|
55 | getInfo_UNSTABLE<T>(recoilValue: RecoilValue<T>): AtomInfo<T>;
|
56 | map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot;
|
57 | asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise<void>): Promise<Snapshot>;
|
58 | retain(): () => void;
|
59 | }
|
60 |
|
61 | export class MutableSnapshot extends Snapshot {
|
62 | set: SetRecoilState;
|
63 | reset: ResetRecoilState;
|
64 | }
|
65 |
|
66 |
|
67 | export type AtomEffect<T> = (param: {
|
68 | node: RecoilState<T>,
|
69 | trigger: 'set' | 'get',
|
70 |
|
71 |
|
72 | setSelf: (param:
|
73 | | T
|
74 | | DefaultValue
|
75 | | Promise<T | DefaultValue>
|
76 | | ((param: T | DefaultValue) => T | DefaultValue),
|
77 | ) => void,
|
78 | resetSelf: () => void,
|
79 |
|
80 |
|
81 |
|
82 | onSet: (
|
83 | param: (newValue: T, oldValue: T | DefaultValue) => void,
|
84 | ) => void,
|
85 | }) => void | (() => void);
|
86 |
|
87 | // atom.d.ts
|
88 | export interface AtomOptions<T> {
|
89 | key: NodeKey;
|
90 | default: RecoilValue<T> | Promise<T> | T;
|
91 | effects_UNSTABLE?: ReadonlyArray<AtomEffect<T>>;
|
92 | dangerouslyAllowMutability?: boolean;
|
93 | }
|
94 |
|
95 | /**
|
96 | * Creates an atom, which represents a piece of writeable state
|
97 | */
|
98 | export function atom<T>(options: AtomOptions<T>): RecoilState<T>;
|
99 |
|
100 | // selector.d.ts
|
101 | export type GetRecoilValue = <T>(recoilVal: RecoilValue<T>) => T;
|
102 | export type GetCallback = <Args extends ReadonlyArray<unknown>, Return>(
|
103 | fn: (interface: Readonly<{snapshot: Snapshot}>) => (...args: Args) => Return,
|
104 | ) => (...args: Args) => Return;
|
105 |
|
106 | export type SetRecoilState = <T>(
|
107 | recoilVal: RecoilState<T>,
|
108 | newVal: T | DefaultValue | ((prevValue: T) => T | DefaultValue),
|
109 | ) => void;
|
110 |
|
111 | export type ResetRecoilState = (recoilVal: RecoilState<any>) => void;
|
112 |
|
113 |
|
114 |
|
115 | export type EvictionPolicy = 'lru' | 'keep-all' | 'most-recent';
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | export type CachePolicyWithoutEquality = {eviction: 'lru', maxSize: number} | {eviction: 'keep-all'} | {eviction: 'most-recent'};
|
129 |
|
130 | export interface ReadOnlySelectorOptions<T> {
|
131 | key: string;
|
132 | get: (opts: {
|
133 | get: GetRecoilValue,
|
134 | getCallback: GetCallback,
|
135 | }) => Promise<T> | RecoilValue<T> | T;
|
136 | dangerouslyAllowMutability?: boolean;
|
137 | cachePolicy_UNSTABLE?: CachePolicyWithoutEquality;
|
138 | }
|
139 |
|
140 | export interface ReadWriteSelectorOptions<T> extends ReadOnlySelectorOptions<T> {
|
141 | set: (
|
142 | opts: {
|
143 | set: SetRecoilState;
|
144 | get: GetRecoilValue;
|
145 | reset: ResetRecoilState;
|
146 | },
|
147 | newValue: T | DefaultValue,
|
148 | ) => void;
|
149 | }
|
150 |
|
151 | export function selector<T>(options: ReadWriteSelectorOptions<T>): RecoilState<T>;
|
152 | export function selector<T>(options: ReadOnlySelectorOptions<T>): RecoilValueReadOnly<T>;
|
153 |
|
154 |
|
155 | export type SetterOrUpdater<T> = (valOrUpdater: ((currVal: T) => T) | T) => void;
|
156 | export type Resetter = () => void;
|
157 | export interface TransactionInterface_UNSTABLE {
|
158 | get<T>(a: RecoilValue<T>): T;
|
159 | set<T>(s: RecoilState<T>, u: ((currVal: T) => T) | T): void;
|
160 | reset(s: RecoilState<any>): void;
|
161 | }
|
162 | export type CallbackInterface = Readonly<{
|
163 | set: <T>(recoilVal: RecoilState<T>, valOrUpdater: ((currVal: T) => T) | T) => void;
|
164 | reset: (recoilVal: RecoilState<any>) => void;
|
165 | snapshot: Snapshot,
|
166 | gotoSnapshot: (snapshot: Snapshot) => void,
|
167 | transact_UNSTABLE: (cb: (i: TransactionInterface_UNSTABLE) => void) => void;
|
168 | }>;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export function useRecoilValue<T>(recoilValue: RecoilValue<T>): T;
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export function useRecoilValueLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | export function useRecoilState<T>(recoilState: RecoilState<T>): [T, SetterOrUpdater<T>];
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | export function useRecoilStateLoadable<T>(recoilState: RecoilState<T>): [Loadable<T>, SetterOrUpdater<T>];
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | export function useSetRecoilState<T>(recoilState: RecoilState<T>): SetterOrUpdater<T>;
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | export function useResetRecoilState(recoilState: RecoilState<any>): Resetter;
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | export function useGetRecoilValueInfo_UNSTABLE(): <T>(recoilValue: RecoilValue<T>) => AtomInfo<T>;
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | export function useRecoilCallback<Args extends ReadonlyArray<unknown>, Return>(
|
220 | fn: (interface: CallbackInterface) => (...args: Args) => Return,
|
221 | deps?: ReadonlyArray<unknown>,
|
222 | ): (...args: Args) => Return;
|
223 |
|
224 | export function useRecoilTransaction_UNSTABLE<Args extends ReadonlyArray<unknown>>(
|
225 | fn: (interface: TransactionInterface_UNSTABLE) => (...args: Args) => void,
|
226 | deps?: ReadonlyArray<unknown>,
|
227 | ): (...args: Args) => void;
|
228 |
|
229 | export function useRecoilTransactionObserver_UNSTABLE(
|
230 | callback: (opts: {
|
231 | snapshot: Snapshot,
|
232 | previousSnapshot: Snapshot,
|
233 | }) => void,
|
234 | ): void;
|
235 |
|
236 | export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void;
|
237 |
|
238 | export function useRecoilSnapshot(): Snapshot;
|
239 |
|
240 |
|
241 | export const RecoilBridge: React.FC;
|
242 | export function useRecoilBridgeAcrossReactRoots_UNSTABLE(): typeof RecoilBridge;
|
243 |
|
244 |
|
245 | declare const LoadablePromiseValue_OPAQUE: unique symbol;
|
246 | interface LoadablePromiseValue {
|
247 | readonly [LoadablePromiseValue_OPAQUE]: true;
|
248 | }
|
249 | type LoadablePromise<T> = Promise<LoadablePromiseValue>;
|
250 |
|
251 | interface BaseLoadable<T> {
|
252 | getValue: () => T;
|
253 | toPromise: () => Promise<T>;
|
254 | valueOrThrow: () => T;
|
255 | errorOrThrow: () => any;
|
256 | promiseOrThrow: () => Promise<T>;
|
257 | is: (other: Loadable<any>) => boolean;
|
258 | map: <S>(map: (from: T) => Promise<S> | S) => Loadable<S>;
|
259 | }
|
260 |
|
261 | interface ValueLoadable<T> extends BaseLoadable<T> {
|
262 | state: 'hasValue';
|
263 | contents: T;
|
264 | valueMaybe: () => T;
|
265 | errorMaybe: () => undefined;
|
266 | promiseMaybe: () => undefined;
|
267 | }
|
268 |
|
269 | interface LoadingLoadable<T> extends BaseLoadable<T> {
|
270 | state: 'loading';
|
271 | contents: LoadablePromise<T>;
|
272 | valueMaybe: () => undefined;
|
273 | errorMaybe: () => undefined;
|
274 | promiseMaybe: () => Promise<T>;
|
275 | }
|
276 |
|
277 | interface ErrorLoadable<T> extends BaseLoadable<T> {
|
278 | state: 'hasError';
|
279 | contents: any;
|
280 | valueMaybe: () => undefined;
|
281 | errorMaybe: () => any;
|
282 | promiseMaybe: () => undefined;
|
283 | }
|
284 |
|
285 | export type Loadable<T> =
|
286 | | ValueLoadable<T>
|
287 | | LoadingLoadable<T>
|
288 | | ErrorLoadable<T>;
|
289 |
|
290 |
|
291 | declare class AbstractRecoilValue<T> {
|
292 | __tag: [T];
|
293 | __cTag: (t: T) => void;
|
294 |
|
295 | key: NodeKey;
|
296 | constructor(newKey: NodeKey);
|
297 | }
|
298 |
|
299 | declare class AbstractRecoilValueReadonly<T> {
|
300 | __tag: [T];
|
301 |
|
302 | key: NodeKey;
|
303 | constructor(newKey: NodeKey);
|
304 | }
|
305 |
|
306 | export class RecoilState<T> extends AbstractRecoilValue<T> {}
|
307 | export class RecoilValueReadOnly<T> extends AbstractRecoilValueReadonly<T> {}
|
308 | export type RecoilValue<T> = RecoilValueReadOnly<T> | RecoilState<T>;
|
309 |
|
310 | export function isRecoilValue(val: unknown): val is RecoilValue<any>;
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | type Primitive = undefined | null | boolean | number | symbol | string;
|
316 |
|
317 | export type SerializableParam =
|
318 | | Primitive
|
319 | | {toJSON: () => string}
|
320 | | ReadonlyArray<SerializableParam>
|
321 | | Readonly<{[key: string]: SerializableParam}>;
|
322 |
|
323 | export interface AtomFamilyOptions<T, P extends SerializableParam> {
|
324 | key: NodeKey;
|
325 | dangerouslyAllowMutability?: boolean;
|
326 | default: RecoilValue<T> | Promise<T> | T | ((param: P) => T | RecoilValue<T> | Promise<T>);
|
327 | effects_UNSTABLE?: | ReadonlyArray<AtomEffect<T>> | ((param: P) => ReadonlyArray<AtomEffect<T>>);
|
328 | // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API
|
329 | }
|
330 |
|
331 | export function atomFamily<T, P extends SerializableParam>(
|
332 | options: AtomFamilyOptions<T, P>,
|
333 | ): (param: P) => RecoilState<T>;
|
334 |
|
335 | export interface ReadOnlySelectorFamilyOptions<T, P extends SerializableParam> {
|
336 | key: string;
|
337 | get: (param: P) => (opts: {
|
338 | get: GetRecoilValue,
|
339 | getCallback: GetCallback,
|
340 | }) => Promise<T> | RecoilValue<T> | T;
|
341 |
|
342 | cachePolicy_UNSTABLE?: CachePolicyWithoutEquality;
|
343 | dangerouslyAllowMutability?: boolean;
|
344 | }
|
345 |
|
346 | export interface ReadWriteSelectorFamilyOptions<T, P extends SerializableParam> {
|
347 | key: string;
|
348 | get: (param: P) => (opts: {
|
349 | get: GetRecoilValue,
|
350 | getCallback: GetCallback,
|
351 | }) => Promise<T> | RecoilValue<T> | T;
|
352 | set: (
|
353 | param: P,
|
354 | ) => (
|
355 | opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState },
|
356 | newValue: T | DefaultValue,
|
357 | ) => void;
|
358 |
|
359 | cachePolicy_UNSTABLE?: CachePolicyWithoutEquality;
|
360 | dangerouslyAllowMutability?: boolean;
|
361 | }
|
362 |
|
363 | export function selectorFamily<T, P extends SerializableParam>(
|
364 | options: ReadWriteSelectorFamilyOptions<T, P>,
|
365 | ): (param: P) => RecoilState<T>;
|
366 |
|
367 | export function selectorFamily<T, P extends SerializableParam>(
|
368 | options: ReadOnlySelectorFamilyOptions<T, P>,
|
369 | ): (param: P) => RecoilValueReadOnly<T>;
|
370 |
|
371 | export function constSelector<T extends SerializableParam>(constant: T): RecoilValueReadOnly<T>;
|
372 |
|
373 | export function errorSelector(message: string): RecoilValueReadOnly<never>;
|
374 |
|
375 | export function readOnlySelector<T>(atom: RecoilValue<T>): RecoilValueReadOnly<T>;
|
376 |
|
377 | export function noWait<T>(state: RecoilValue<T>): RecoilValueReadOnly<Loadable<T>>;
|
378 |
|
379 |
|
380 |
|
381 | export type UnwrapRecoilValue<T> = T extends RecoilValue<infer R> ? R : never;
|
382 |
|
383 | export type UnwrapRecoilValues<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
|
384 | [P in keyof T]: UnwrapRecoilValue<T[P]>;
|
385 | };
|
386 | export type UnwrapRecoilValueLoadables<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
|
387 | [P in keyof T]: Loadable<UnwrapRecoilValue<T[P]>>;
|
388 | };
|
389 |
|
390 | export function waitForNone<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
|
391 | param: RecoilValues,
|
392 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
393 |
|
394 | export function waitForNone<RecoilValues extends { [key: string]: RecoilValue<any> }>(
|
395 | param: RecoilValues,
|
396 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
397 |
|
398 | export function waitForAny<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
|
399 | param: RecoilValues,
|
400 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
401 |
|
402 | export function waitForAny<RecoilValues extends { [key: string]: RecoilValue<any> }>(
|
403 | param: RecoilValues,
|
404 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
405 |
|
406 | export function waitForAll<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
|
407 | param: RecoilValues,
|
408 | ): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
|
409 |
|
410 | export function waitForAll<RecoilValues extends { [key: string]: RecoilValue<any> }>(
|
411 | param: RecoilValues,
|
412 | ): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
|
413 |
|
414 | export function waitForAllSettled<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
|
415 | param: RecoilValues,
|
416 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
417 |
|
418 | export function waitForAllSettled<RecoilValues extends { [key: string]: RecoilValue<any> }>(
|
419 | param: RecoilValues,
|
420 | ): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
|
421 |
|
422 |
|
423 |
|
424 | export function snapshot_UNSTABLE(initializeState?: (shapshot: MutableSnapshot) => void): Snapshot;
|