/** * 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. * * A type that represents a value that may or may not be loaded. It differs from * LoadObject in that the `loading` state has a Promise that is meant to resolve * when the value is available (but as with LoadObject, an individual Loadable * is a value type and is not mutated when the status of a request changes). * * @emails oncall+recoil * @flow strict * @format */ 'use strict'; const err = require('recoil-shared/util/Recoil_err'); const isPromise = require('recoil-shared/util/Recoil_isPromise'); const nullthrows = require('recoil-shared/util/Recoil_nullthrows'); declare class BaseLoadable { getValue(): T, toPromise(): Promise, valueMaybe(): T | void, valueOrThrow(): T, promiseMaybe(): Promise | void, promiseOrThrow(): Promise, errorMaybe(): mixed | void, errorOrThrow(): mixed, is(other: Loadable): boolean, map(_map: (T) => Promise | Loadable | S): Loadable, } declare class ValueLoadable extends BaseLoadable { state: 'hasValue', contents: T, constructor(value: T): any, getValue(): T, toPromise(): Promise, valueMaybe(): T, valueOrThrow(): T, promiseMaybe(): void, errorMaybe(): void, map(map: (T) => Promise | Loadable | S): Loadable, } declare class ErrorLoadable extends BaseLoadable { state: 'hasError', contents: mixed, constructor(error: mixed): any, getValue(): T, toPromise(): Promise, valueMaybe(): void, promiseMaybe(): void, errorMaybe(): mixed, errorOrThrow(): mixed, map(_map: (T) => Promise | Loadable | S): $ReadOnly>, } declare class LoadingLoadable extends BaseLoadable { state: 'loading', contents: Promise, constructor(promise: Promise): any, getValue(): T, toPromise(): Promise, valueMaybe(): void, promiseMaybe(): Promise, promiseOrThrow(): Promise, errorMaybe(): void, map(map: (T) => Promise | Loadable | S): $ReadOnly>, } export type Loadable<+T> = $ReadOnly> | $ReadOnly> | $ReadOnly>; export type ValueLoadableType<+T> = $ReadOnly>; export type ErrorLoadableType<+T> = $ReadOnly>; export type LoadingLoadableType<+T> = $ReadOnly>; declare function loadableWithValue<+T>(value: T): $ReadOnly>; declare function loadableWithError<+T>(error: mixed): $ReadOnly>; declare function loadableWithPromise<+T>(promise: Promise): $ReadOnly>; declare function loadableLoading<+T>(): $ReadOnly>; type UnwrapLoadables = $TupleMap(Loadable) => T>; type LoadableAllOfTuple = | Promise | mixed>>(tuple: Tuple) => Loadable<$TupleMap(Loadable | Promise | V) => V>>; type LoadableAllOfObj = | Promise | mixed, ... }>>(obj: Obj) => Loadable<$ObjMap(Loadable | Promise | V) => V>>; type LoadableAll = LoadableAllOfTuple & LoadableAllOfObj; declare function loadableAllArray>>(inputs: Inputs): Loadable>; declare function loadableAll | Promise | mixed> | $ReadOnly<{ [string]: Loadable | Promise | mixed, ... }>>(inputs: Inputs): Loadable<$ReadOnlyArray | $ReadOnly<{ [string]: mixed, ... }>>; declare function isLoadable(x: mixed): boolean; const LoadableStaticInterface = { of: (value: Promise | Loadable | T): Loadable => isPromise(value) ? loadableWithPromise(value) : isLoadable(value) ? value : loadableWithValue(value), error: (error: mixed): $ReadOnly> => loadableWithError(error), // $FlowIssue[unclear-type] all: ((loadableAll: any): LoadableAll), isLoadable }; module.exports = { loadableWithValue, loadableWithError, loadableWithPromise, loadableLoading, loadableAll, isLoadable, RecoilLoadable: LoadableStaticInterface };