Skip to content
Myr

index.js.flow

This file

Path

src/types/index.js.flow

126 lines · 4.3 KiB

Last changed by

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

History of this file

Take it

1// @flow23export interface Patch {4	op: "replace" | "remove" | "add";5	path: (string | number)[];6	value?: any;7}89export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void1011type Base = {...} | Array<any>12interface IProduce {13	/**14	 * Immer takes a state, and runs a function against it.15	 * That function can freely mutate the state, as it will create copies-on-write.16	 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.17	 *18	 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe19	 * any time it is called with the current state.20	 *21	 * @param currentState - the state to start with22	 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified23	 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined24	 * @returns The next state: a new state, or the current state if nothing was modified25	 */26	<S: Base>(27		currentState: S,28		recipe: (draftState: S) => S | void,29		patchListener?: PatchListener30	): S;31	// curried invocations with initial state32	<S: Base, A = void, B = void, C = void>(33		recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,34		initialState: S35	): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => S;36	// curried invocations without initial state37	<S: Base, A = void, B = void, C = void>(38		recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void39	): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S;40}4142interface IProduceWithPatches {43	/**44	 * Like `produce`, but instead of just returning the new state,45	 * a tuple is returned with [nextState, patches, inversePatches]46	 *47	 * Like produce, this function supports currying48	 */49	<S: Base>(50		currentState: S,51		recipe: (draftState: S) => S | void52	): [S, Patch[], Patch[]];53	// curried invocations with initial state54	<S: Base, A = void, B = void, C = void>(55		recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,56		initialState: S57	): (58		currentState: S | void,59		a: A,60		b: B,61		c: C,62		...extraArgs: any[]63	) => [S, Patch[], Patch[]];64	// curried invocations without initial state65	<S: Base, A = void, B = void, C = void>(66		recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void67	): (68		currentState: S,69		a: A,70		b: B,71		c: C,72		...extraArgs: any[]73	) => [S, Patch[], Patch[]];74}7576declare export var produce: IProduce7778declare export var produceWithPatches: IProduceWithPatches7980declare export var nothing: typeof undefined8182declare export var immerable: Symbol8384/**85 * Automatically freezes any state trees generated by immer.86 * This protects against accidental modifications of the state tree outside of an immer function.87 * This comes with a performance impact, so it is recommended to disable this option in production.88 * By default it is turned on during local development, and turned off in production.89 */90declare export function setAutoFreeze(autoFreeze: boolean): void9192/**93 * Pass false to disable strict shallow copy.94 *95 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.96 */97declare export function setUseStrictShallowCopy(98	useStrictShallowCopy: boolean99): void100101declare export function applyPatches<S>(state: S, patches: Patch[]): S102103declare export function original<S>(value: S): S104105declare export function current<S>(value: S): S106107declare export function isDraft(value: any): boolean108109/**110 * Creates a mutable draft from an (immutable) object / array.111 * The draft can be modified until `finishDraft` is called112 */113declare export function createDraft<T>(base: T): T114115/**116 * Given a draft that was created using `createDraft`,117 * finalizes the draft into a new immutable object.118 * Optionally a patch-listener can be provided to gather the patches that are needed to construct the object.119 */120declare export function finishDraft<T>(base: T, listener?: PatchListener): T121122declare export function enableMapSet(): void123declare export function enablePatches(): void124declare export function enableArrayMethods(): void125126declare export function freeze<T>(obj: T, freeze?: boolean): T