| 1 | // @flow
|
|---|
| 2 |
|
|---|
| 3 | export interface Patch {
|
|---|
| 4 | op: "replace" | "remove" | "add";
|
|---|
| 5 | path: (string | number)[];
|
|---|
| 6 | value?: any;
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void
|
|---|
| 10 |
|
|---|
| 11 | type Base = {...} | Array<any>
|
|---|
| 12 | interface 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 recipe
|
|---|
| 19 | * any time it is called with the current state.
|
|---|
| 20 | *
|
|---|
| 21 | * @param currentState - the state to start with
|
|---|
| 22 | * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
|
|---|
| 23 | * @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 undefined
|
|---|
| 24 | * @returns The next state: a new state, or the current state if nothing was modified
|
|---|
| 25 | */
|
|---|
| 26 | <S: Base>(
|
|---|
| 27 | currentState: S,
|
|---|
| 28 | recipe: (draftState: S) => S | void,
|
|---|
| 29 | patchListener?: PatchListener
|
|---|
| 30 | ): S;
|
|---|
| 31 | // curried invocations with initial state
|
|---|
| 32 | <S: Base, A = void, B = void, C = void>(
|
|---|
| 33 | recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,
|
|---|
| 34 | initialState: S
|
|---|
| 35 | ): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => S;
|
|---|
| 36 | // curried invocations without initial state
|
|---|
| 37 | <S: Base, A = void, B = void, C = void>(
|
|---|
| 38 | recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void
|
|---|
| 39 | ): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | interface 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 currying
|
|---|
| 48 | */
|
|---|
| 49 | <S: Base>(
|
|---|
| 50 | currentState: S,
|
|---|
| 51 | recipe: (draftState: S) => S | void
|
|---|
| 52 | ): [S, Patch[], Patch[]];
|
|---|
| 53 | // curried invocations with initial state
|
|---|
| 54 | <S: Base, A = void, B = void, C = void>(
|
|---|
| 55 | recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,
|
|---|
| 56 | initialState: S
|
|---|
| 57 | ): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => [S, Patch[], Patch[]];
|
|---|
| 58 | // curried invocations without initial state
|
|---|
| 59 | <S: Base, A = void, B = void, C = void>(
|
|---|
| 60 | recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void
|
|---|
| 61 | ): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => [S, Patch[], Patch[]];
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | declare export var produce: IProduce
|
|---|
| 65 |
|
|---|
| 66 | declare export var produceWithPatches: IProduceWithPatches
|
|---|
| 67 |
|
|---|
| 68 | declare export var nothing: typeof undefined
|
|---|
| 69 |
|
|---|
| 70 | declare export var immerable: Symbol
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Automatically freezes any state trees generated by immer.
|
|---|
| 74 | * This protects against accidental modifications of the state tree outside of an immer function.
|
|---|
| 75 | * This comes with a performance impact, so it is recommended to disable this option in production.
|
|---|
| 76 | * By default it is turned on during local development, and turned off in production.
|
|---|
| 77 | */
|
|---|
| 78 | declare export function setAutoFreeze(autoFreeze: boolean): void
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Pass false to disable strict shallow copy.
|
|---|
| 82 | *
|
|---|
| 83 | * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
|---|
| 84 | */
|
|---|
| 85 | declare export function setUseStrictShallowCopy(useStrictShallowCopy: boolean): void
|
|---|
| 86 |
|
|---|
| 87 | declare export function applyPatches<S>(state: S, patches: Patch[]): S
|
|---|
| 88 |
|
|---|
| 89 | declare export function original<S>(value: S): S
|
|---|
| 90 |
|
|---|
| 91 | declare export function current<S>(value: S): S
|
|---|
| 92 |
|
|---|
| 93 | declare export function isDraft(value: any): boolean
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Creates a mutable draft from an (immutable) object / array.
|
|---|
| 97 | * The draft can be modified until `finishDraft` is called
|
|---|
| 98 | */
|
|---|
| 99 | declare export function createDraft<T>(base: T): T
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Given a draft that was created using `createDraft`,
|
|---|
| 103 | * finalizes the draft into a new immutable object.
|
|---|
| 104 | * Optionally a patch-listener can be provided to gather the patches that are needed to construct the object.
|
|---|
| 105 | */
|
|---|
| 106 | declare export function finishDraft<T>(base: T, listener?: PatchListener): T
|
|---|
| 107 |
|
|---|
| 108 | declare export function enableMapSet(): void
|
|---|
| 109 | declare export function enablePatches(): void
|
|---|
| 110 |
|
|---|
| 111 | declare export function freeze<T>(obj: T, freeze?: boolean): T
|
|---|