1 | import {
|
2 | IProduce,
|
3 | IProduceWithPatches,
|
4 | Immer,
|
5 | Draft,
|
6 | Immutable
|
7 | } from "./internal"
|
8 |
|
9 | export {
|
10 | Draft,
|
11 | WritableDraft,
|
12 | Immutable,
|
13 | Patch,
|
14 | PatchListener,
|
15 | Producer,
|
16 | original,
|
17 | current,
|
18 | isDraft,
|
19 | isDraftable,
|
20 | NOTHING as nothing,
|
21 | DRAFTABLE as immerable,
|
22 | freeze,
|
23 | Objectish,
|
24 | StrictMode
|
25 | } from "./internal"
|
26 |
|
27 | const immer = new Immer()
|
28 |
|
29 | /**
|
30 | * The `produce` function takes a value and a "recipe function" (whose
|
31 | * return value often depends on the base state). The recipe function is
|
32 | * free to mutate its first argument however it wants. All mutations are
|
33 | * only ever applied to a __copy__ of the base state.
|
34 | *
|
35 | * Pass only a function to create a "curried producer" which relieves you
|
36 | * from passing the recipe function every time.
|
37 | *
|
38 | * Only plain objects and arrays are made mutable. All other objects are
|
39 | * considered uncopyable.
|
40 | *
|
41 | * Note: This function is __bound__ to its `Immer` instance.
|
42 | *
|
43 | * @param {any} base - the initial state
|
44 | * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
|
45 | * @param {Function} patchListener - optional function that will be called with all the patches produced here
|
46 | * @returns {any} a new state, or the initial state if nothing was modified
|
47 | */
|
48 | export const produce: IProduce = immer.produce
|
49 |
|
50 | /**
|
51 | * Like `produce`, but `produceWithPatches` always returns a tuple
|
52 | * [nextState, patches, inversePatches] (instead of just the next state)
|
53 | */
|
54 | export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(
|
55 | immer
|
56 | )
|
57 |
|
58 | /**
|
59 | * Pass true to automatically freeze all copies created by Immer.
|
60 | *
|
61 | * Always freeze by default, even in production mode
|
62 | */
|
63 | export const setAutoFreeze = immer.setAutoFreeze.bind(immer)
|
64 |
|
65 | /**
|
66 | * Pass true to enable strict shallow copy.
|
67 | *
|
68 | * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
69 | */
|
70 | export const setUseStrictShallowCopy = immer.setUseStrictShallowCopy.bind(immer)
|
71 |
|
72 | /**
|
73 | * Apply an array of Immer patches to the first argument.
|
74 | *
|
75 | * This function is a producer, which means copy-on-write is in effect.
|
76 | */
|
77 | export const applyPatches = immer.applyPatches.bind(immer)
|
78 |
|
79 | /**
|
80 | * Create an Immer draft from the given base state, which may be a draft itself.
|
81 | * The draft can be modified until you finalize it with the `finishDraft` function.
|
82 | */
|
83 | export const createDraft = immer.createDraft.bind(immer)
|
84 |
|
85 | /**
|
86 | * Finalize an Immer draft from a `createDraft` call, returning the base state
|
87 | * (if no changes were made) or a modified copy. The draft must *not* be
|
88 | * mutated afterwards.
|
89 | *
|
90 | * Pass a function as the 2nd argument to generate Immer patches based on the
|
91 | * changes that were made.
|
92 | */
|
93 | export const finishDraft = immer.finishDraft.bind(immer)
|
94 |
|
95 | /**
|
96 | * This function is actually a no-op, but can be used to cast an immutable type
|
97 | * to an draft type and make TypeScript happy
|
98 | *
|
99 | * @param value
|
100 | */
|
101 | export function castDraft<T>(value: T): Draft<T> {
|
102 | return value as any
|
103 | }
|
104 |
|
105 | /**
|
106 | * This function is actually a no-op, but can be used to cast a mutable type
|
107 | * to an immutable type and make TypeScript happy
|
108 | * @param value
|
109 | */
|
110 | export function castImmutable<T>(value: T): Immutable<T> {
|
111 | return value as any
|
112 | }
|
113 |
|
114 | export {Immer}
|
115 |
|
116 | export {enablePatches} from "./plugins/patches"
|
117 | export {enableMapSet} from "./plugins/mapset"
|