| [a762898] | 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ immer.setUseStrictShallowCopy.bind(
|
|---|
| 71 | immer
|
|---|
| 72 | )
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Pass false to use loose iteration that only processes enumerable string properties.
|
|---|
| 76 | * This skips symbols and non-enumerable properties for maximum performance.
|
|---|
| 77 | *
|
|---|
| 78 | * By default, strict iteration is enabled (includes all own properties).
|
|---|
| 79 | */
|
|---|
| 80 | export const setUseStrictIteration = /* @__PURE__ */ immer.setUseStrictIteration.bind(
|
|---|
| 81 | immer
|
|---|
| 82 | )
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Apply an array of Immer patches to the first argument.
|
|---|
| 86 | *
|
|---|
| 87 | * This function is a producer, which means copy-on-write is in effect.
|
|---|
| 88 | */
|
|---|
| 89 | export const applyPatches = /* @__PURE__ */ immer.applyPatches.bind(immer)
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Create an Immer draft from the given base state, which may be a draft itself.
|
|---|
| 93 | * The draft can be modified until you finalize it with the `finishDraft` function.
|
|---|
| 94 | */
|
|---|
| 95 | export const createDraft = /* @__PURE__ */ immer.createDraft.bind(immer)
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Finalize an Immer draft from a `createDraft` call, returning the base state
|
|---|
| 99 | * (if no changes were made) or a modified copy. The draft must *not* be
|
|---|
| 100 | * mutated afterwards.
|
|---|
| 101 | *
|
|---|
| 102 | * Pass a function as the 2nd argument to generate Immer patches based on the
|
|---|
| 103 | * changes that were made.
|
|---|
| 104 | */
|
|---|
| 105 | export const finishDraft = /* @__PURE__ */ immer.finishDraft.bind(immer)
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * This function is actually a no-op, but can be used to cast an immutable type
|
|---|
| 109 | * to an draft type and make TypeScript happy
|
|---|
| 110 | *
|
|---|
| 111 | * @param value
|
|---|
| 112 | */
|
|---|
| 113 | export function castDraft<T>(value: T): Draft<T> {
|
|---|
| 114 | return value as any
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * This function is actually a no-op, but can be used to cast a mutable type
|
|---|
| 119 | * to an immutable type and make TypeScript happy
|
|---|
| 120 | * @param value
|
|---|
| 121 | */
|
|---|
| 122 | export function castImmutable<T>(value: T): Immutable<T> {
|
|---|
| 123 | return value as any
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | export {Immer}
|
|---|
| 127 |
|
|---|
| 128 | export {enablePatches} from "./plugins/patches"
|
|---|
| 129 | export {enableMapSet} from "./plugins/mapset"
|
|---|