UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

439 lines (341 loc) 18.1 kB
export declare type AnyArray = Array<any>; declare type AnyFunc = (...args: any[]) => any; export declare type AnyMap = Map<any, any>; export declare type AnyObject = { [key: string]: any; }; export declare type AnySet = Set<any>; export declare const enum Archtype { Object = 0, Array = 1, Map = 2, Set = 3 } declare type AtomicObject = Function | Promise<any> | Date | RegExp; export declare function createProxy<T extends Objectish>(immer: ImmutableManage, value: T, parent?: ImmutableType): Drafted<T, ImmutableType>; /** * Returns a new draft of the `base` object. * * The second argument is the parent draft-state (used internally). */ export declare function createProxyDraft<T extends Objectish>(base: T, parent?: ImmutableType): Drafted<T, ProxyState>; /** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */ export declare function current<T>(value: T): T; export declare type Draft<T> = T extends PrimitiveType ? T : T extends AtomicObject ? T : T extends IfAvailable<ReadonlyMap<infer K, infer V>> ? Map<Draft<K>, Draft<V>> : T extends IfAvailable<ReadonlySet<infer V>> ? Set<Draft<V>> : T extends WeakReferences ? T : T extends object ? WritableDraft<T> : T; export declare const DRAFT_STATE: unique symbol; /** * To let Immer treat your class instances as plain immutable objects * (albeit with a custom prototype), you must define either an instance property * or a static property on each of your custom classes. * * Otherwise, your class instance will never be drafted, which means it won't be * safe to mutate in a produce callback. */ export declare const DRAFTABLE: unique symbol; export declare type Drafted<Base = any, T extends ImmutableType = ImmutableType> = { [DRAFT_STATE]: T; } & Base; export declare type DraftFunction<S> = (draft: Draft<S>) => void; export declare function eachProperty<T extends Objectish>(obj: T, iter: (key: string | number, value: any, source: T) => void, enumerableOnly?: boolean): void; export declare function enableAllPlugins(): void; export declare function enableES5(): void; export declare function enableMapSet(): void; export declare function enablePatches(): void; export declare function enterScope(immer: ImmutableManage): ImmutableScope; export declare interface ES5ArrayState extends ES5BaseState { type_: ProxyType.ES5Array; draft_: Drafted<AnyObject, ES5ArrayState>; base_: any; copy_: any; } declare interface ES5BaseState extends ImmutableBaseState { assigned_: { [key: string]: any; }; parent_?: ImmutableType; revoked_: boolean; } export declare interface ES5ObjectState extends ES5BaseState { type_: ProxyType.ES5Object; draft_: Drafted<AnyObject, ES5ObjectState>; base_: AnyObject; copy_: AnyObject | null; } export declare type ES5State = ES5ArrayState | ES5ObjectState; /** * Freezes draftable objects. Returns the original object. * By default freezes shallowly, but if the second argument is `true` it will freeze recursively. * * @param obj * @param deep */ export declare function freeze<T>(obj: T, deep?: boolean): T; declare type FromNothing<T> = T extends Nothing ? undefined : T; export declare function getArchtype(thing: any): Archtype; export declare function getCurrentScope(): ImmutableScope; export declare const getOwnPropertyDescriptors: <T>(o: T) => { [P in keyof T]: TypedPropertyDescriptor<T[P]>; } & { [x: string]: PropertyDescriptor; }; export declare function getPlugin<K extends keyof Plugins>(pluginKey: K): Exclude<Plugins[K], undefined>; export declare function getProperty(thing: AnyMap | AnyObject, prop: PropertyKey): any; export declare const hasMap: boolean; export declare function hasProperty(thing: any, prop: PropertyKey): boolean; export declare const hasProxies: boolean; export declare const hasSet: boolean; export declare type IfAvailable<T, Fallback = void> = true | false extends (T extends never ? true : false) ? Fallback : keyof T extends never ? Fallback : T; /** Convert a mutable type into a readonly type */ export declare type Immutable<T> = T extends PrimitiveType ? T : T extends AtomicObject ? T : T extends IfAvailable<ReadonlyMap<infer K, infer V>> ? ReadonlyMap<Immutable<K>, Immutable<V>> : T extends IfAvailable<ReadonlySet<infer V>> ? ReadonlySet<Immutable<V>> : T extends WeakReferences ? T : T extends object ? { readonly [K in keyof T]: Immutable<T[K]>; } : T; export declare interface ImmutableBaseState { parent_?: ImmutableType; scope_: ImmutableScope; modified_: boolean; finalized_: boolean; isManual_: boolean; } export declare type ImmutableDispatch<S> = (arg: S | DraftFunction<S>) => void; export declare class ImmutableManage implements ProducersFns { useProxies_: boolean; autoFreeze_: boolean; constructor(config?: { useProxies?: boolean; autoFreeze?: boolean; }); /** * The `produce` function takes a value and a "recipe function" (whose * return value often depends on the base state). The recipe function is * free to mutate its first argument however it wants. All mutations are * only ever applied to a __copy__ of the base state. * * Pass only a function to create a "curried producer" which relieves you * from passing the recipe function every time. * * Only plain objects and arrays are made mutable. All other objects are * considered uncopyable. * * Note: This function is __bound__ to its `Immer` instance. * * @param {any} base - the initial state * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @param {Function} patchListener - optional function that will be called with all the patches produced here * @returns {any} a new state, or the initial state if nothing was modified */ create: ImmutableProduce; produceWithPatches: IProduceWithPatches; createDraft<T extends Objectish>(base: T): Draft<T>; finishDraft<D extends Draft<any>>(draft: D, patchListener?: PatchListener): D extends Draft<infer T> ? T : never; /** * Pass true to automatically freeze all copies * By default, auto-freezing is enabled. */ setAutoFreeze(value: boolean): void; /** * Pass true to use the ES2015 `Proxy` class when creating drafts, which is * always faster than using ES5 proxies. * * By default, feature detection is used, so calling this is rarely necessary. */ setUseProxies(value: boolean): void; applyPatches<T extends Objectish>(base: T, patches: Patch[]): T; } /** * The `produce` function takes a value and a "recipe function" (whose * return value often depends on the base state). The recipe function is * free to mutate its first argument however it wants. All mutations are * only ever applied to a __copy__ of the base state. * * Pass only a function to create a "curried producer" which relieves you * from passing the recipe function every time. * * Only plain objects and arrays are made mutable. All other objects are * considered uncopyable. * * Note: This function is __bound__ to its `Immer` instance. * * @param {any} base - the initial state * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @param {Function} patchListener - optional function that will be called with all the patches produced here * @returns {any} a new state, or the initial state if nothing was modified */ export declare interface ImmutableProduce { /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */ <Curried>(recipe: InferRecipeFromCurried<Curried>, initialState?: InferInitialStateFromCurried<Curried>): Curried; /** Curried producer that infers curried from the recipe */ <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, false>; /** Curried producer that infers curried from the State generic, which is explicitly passed in. */ <State>(recipe: (state: Draft<State>, initialState: State) => ValidRecipeReturnType<State>): (state?: State) => State; <State, Args extends any[]>(recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>, initialState: State): (state?: State, ...args: Args) => State; <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (state: State) => State; <State, Args extends any[]>(recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>): (state: State, ...args: Args) => State; /** Curried producer with initial state, infers recipe from initial state */ <State, Recipe extends Function>(recipe: Recipe, initialState: State): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>; /** Normal producer */ <Base, D = Draft<Base>>(// By using a default inferred D, rather than Draft<Base> in the recipe, we can override it. base: Base, recipe: (draft: D) => ValidRecipeReturnType<D>, listener?: PatchListener): Immutable<Base>; /** Promisified dormal producer */ <Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>, listener?: PatchListener): Promise<Immutable<Base>>; } export declare type ImmutableReducer<S = any, A = any> = (draftState: Draft<S>, action: A) => void | (S extends undefined ? typeof NOTHING : S); export declare interface ImmutableScope { patches_?: Patch[]; inversePatches_?: Patch[]; canAutoFreeze_: boolean; drafts_: any[]; parent_?: ImmutableScope; patchListener_?: PatchListener; immutable_: ImmutableManage; unfinalizedDrafts_: number; } export declare type ImmutableState<S> = [S, ImmutableDispatch<S>]; export declare type ImmutableType = ProxyObjectState | ProxyArrayState | ES5ObjectState | ES5ArrayState | MapState | SetState; declare type InferCurriedFromInitialStateAndRecipe<State, Recipe, UsePatches extends boolean> = Recipe extends (draft: Draft<State>, ...rest: infer RestArgs) => ValidRecipeReturnTypePossiblyPromise<State> ? (base?: State | undefined, ...args: RestArgs) => PromisifyReturnIfNeeded<State, Recipe, UsePatches> : never; declare type InferCurriedFromRecipe<Recipe, UsePatches extends boolean> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any ? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState> ? (base: Immutable<DraftState>, ...args: RestArgs) => PromisifyReturnIfNeeded<DraftState, Recipe, UsePatches> : never : never; declare type InferInitialStateFromCurried<Curried> = Curried extends (base: infer State, ...rest: any[]) => any ? State : never; /** * Core Producer inference */ declare type InferRecipeFromCurried<Curried> = Curried extends (base: infer State, ...rest: infer Args) => any ? ReturnType<Curried> extends State ? (draft: Draft<State>, ...rest: Args) => ValidRecipeReturnType<Draft<State>> : never : never; /** * Like `produce`, but instead of just returning the new state, * a tuple is returned with [nextState, patches, inversePatches] * * Like produce, this function supports currying */ export declare interface IProduceWithPatches { <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>; <State, Recipe extends Function>(recipe: Recipe, initialState: State): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>; <Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => ValidRecipeReturnType<D>, listener?: PatchListener): PatchesTuple<Base>; <Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>, listener?: PatchListener): Promise<PatchesTuple<Base>>; } export declare function isDraft(value: any): boolean; /** Returns true if the given value can be drafted by Immer */ export declare function isDraftable<T>(value: T): boolean; export declare function isEqual(x: any, y: any): boolean; export declare function isFrozen(obj: any): boolean; export declare function isMap(target: any): target is AnyMap; export declare function isPlainObject(value: any): boolean; export declare function isSet(target: any): target is AnySet; export declare const iteratorSymbol: typeof Symbol.iterator; export declare function latest(state: ImmutableType): any; export declare function leaveScope(scope: ImmutableScope): void; export declare function loadPlugin<K extends keyof Plugins>(pluginKey: K, implementation: Plugins[K]): void; export declare interface MapState extends ImmutableBaseState { type_: ProxyType.Map; copy_: AnyMap | undefined; assigned_: Map<any, boolean> | undefined; base_: AnyMap; revoked_: boolean; draft_: Drafted<AnyMap, MapState>; } export declare function markChanged(state: ImmutableType): void; /** * The sentinel value returned by producers to replace the draft with undefined. */ export declare const NOTHING: Nothing; /** Use a class type for `nothing` so its type is unique */ export declare class Nothing { private _; } export declare type Objectish = AnyObject | AnyArray | AnyMap | AnySet; export declare type ObjectishNoSet = AnyObject | AnyArray | AnyMap; /** * Object drafts */ export declare const objectTraps: ProxyHandler<ProxyState>; /** Get the underlying object that is represented by the given draft */ export declare function originalState<T>(value: T): T | undefined; export declare const ownKeys: (target: AnyObject) => PropertyKey[]; export declare interface Patch { op: "replace" | "remove" | "add"; path: (string | number)[]; value?: any; } /** * Utility types */ declare type PatchesTuple<T> = readonly [T, Patch[], Patch[]]; export declare type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void; export declare type PatchPath = (string | number)[]; declare type Plugins = typeof plugins; /** Plugin utilities */ declare const plugins: { Patches?: { generatePatches_(state: ImmutableType, basePath: PatchPath, patches: Patch[], inversePatches: Patch[]): void; generateReplacementPatches_(base: any, replacement: any, patches: Patch[], inversePatches: Patch[]): void; applyPatches_<T>(draft: T, patches: Patch[]): T; }; ES5?: { willFinalizeES5_(scope: ImmutableScope, result: any, isReplaced: boolean): void; createES5Proxy_<T>(base: T, parent?: ImmutableType): Drafted<T, ES5ObjectState | ES5ArrayState>; hasChanges_(state: ES5ArrayState | ES5ObjectState): boolean; }; MapSet?: { proxyMap_<T extends AnyMap>(target: T, parent?: ImmutableType): T; proxySet_<T extends AnySet>(target: T, parent?: ImmutableType): T; }; }; export declare function prepareCopy(state: { base_: any; copy_: any; }): void; declare type PrimitiveType = number | string | boolean; export declare function processResult(result: any, scope: ImmutableScope): any; /** The inferred return type of `produce` */ export declare type Produced<Base, Return> = Return extends void ? Base : Return extends Promise<infer Result> ? Promise<Result extends void ? Base : FromNothing<Result>> : FromNothing<Return>; declare interface ProducersFns { create: ImmutableProduce; produceWithPatches: IProduceWithPatches; } declare type PromisifyReturnIfNeeded<State, Recipe extends AnyFunc, UsePatches extends boolean> = ReturnType<Recipe> extends Promise<any> ? Promise<UsePatches extends true ? PatchesTuple<State> : State> : UsePatches extends true ? PatchesTuple<State> : State; export declare interface ProxyArrayState extends ProxyBaseState { type_: ProxyType.ProxyArray; base_: AnyArray; copy_: AnyArray | null; draft_: Drafted<AnyArray, ProxyArrayState>; } export declare interface ProxyBaseState extends ImmutableBaseState { assigned_: { [property: string]: boolean; }; parent_?: ImmutableType; revoke_(): void; } export declare interface ProxyObjectState extends ProxyBaseState { type_: ProxyType.ProxyObject; base_: any; copy_: any; draft_: Drafted<AnyObject, ProxyObjectState>; } export declare type ProxyState = ProxyObjectState | ProxyArrayState; export declare const enum ProxyType { ProxyObject = 0, ProxyArray = 1, Map = 2, Set = 3, ES5Object = 4, ES5Array = 5 } export declare function revokeScope(scope: ImmutableScope): void; export declare function setProperty(thing: any, propOrOldValue: PropertyKey, value: any): void; export declare interface SetState extends ImmutableBaseState { type_: ProxyType.Set; copy_: AnySet | undefined; base_: AnySet; drafts_: Map<any, Drafted>; revoked_: boolean; draft_: Drafted<AnySet, SetState>; } export declare function shallowCopy(base: any): any; export declare function usePatchesInScope(scope: ImmutableScope, patchListener?: PatchListener): void; declare type ValidRecipeReturnType<State> = State | void | undefined | (State extends undefined ? Nothing : never); declare type ValidRecipeReturnTypePossiblyPromise<State> = ValidRecipeReturnType<State> | Promise<ValidRecipeReturnType<State>>; /** * These should also never be mapped but must be tested after regular Map and * Set */ declare type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>; export declare type WritableDraft<T> = { -readonly [K in keyof T]: Draft<T[K]>; }; export { }