Skip to content
Myr

immer.ts

This file

Path

src/immer.ts

117 lines · 3.3 KiB · TypeScript

Last changed by

fix: export lost types (#1116)Jodobefore adoption5a8f6e7

History of this file

Take it

The walk stopped after 200 commits: lines older than 7faa7b4 point at it.

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 1–10why, one step back
1import {
2 IProduce,3 IProduceWithPatches,4 Immer,5 Draft,6 Immutable7} from "./internal"89export {10 Draft,

fix: export lost types (#1116)Jodobefore adoption5a8f6e7

line 11why, one step back
11 WritableDraft,

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 12–14why, one step back
12 Immutable,
13 Patch,14 PatchListener,

fix: export lost types (#1116)Jodobefore adoption5a8f6e7

line 15why, one step back
15 Producer,

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 16–21why, one step back
16 original,
17 current,18 isDraft,19 isDraftable,20 NOTHING as nothing,21 DRAFTABLE as immerable,

fix: export `Objectish` type (#1043)Milan Jaritzbefore adoption75e004d

line 22why, one step back
22 freeze,

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 23–24why, one step back
23 Objectish,
24 StrictMode

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 25–60why, one step back
25} from "./internal"
2627const immer = new Immer()2829/**30 * The `produce` function takes a value and a "recipe function" (whose31 * return value often depends on the base state). The recipe function is32 * free to mutate its first argument however it wants. All mutations are33 * only ever applied to a __copy__ of the base state.34 *35 * Pass only a function to create a "curried producer" which relieves you36 * from passing the recipe function every time.37 *38 * Only plain objects and arrays are made mutable. All other objects are39 * considered uncopyable.40 *41 * Note: This function is __bound__ to its `Immer` instance.42 *43 * @param {any} base - the initial state44 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified45 * @param {Function} patchListener - optional function that will be called with all the patches produced here46 * @returns {any} a new state, or the initial state if nothing was modified47 */48export const produce: IProduce = immer.produce4950/**51 * Like `produce`, but `produceWithPatches` always returns a tuple52 * [nextState, patches, inversePatches] (instead of just the next state)53 */54export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(55 immer56)5758/**59 * Pass true to automatically freeze all copies created by Immer.60 *

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 61why, one step back
61 * Always freeze by default, even in production mode

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 62–65why, one step back
62 */
63export const setAutoFreeze = immer.setAutoFreeze.bind(immer)6465/**

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 66why, one step back
66 * Pass true to enable strict shallow copy.

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

line 67why, one step back
67 *

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 68why, one step back
68 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

line 69why, one step back
69 */

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 70why, one step back
70export const setUseStrictShallowCopy = immer.setUseStrictShallowCopy.bind(immer)

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

lines 71–117why, one step back
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 */77export const applyPatches = immer.applyPatches.bind(immer)7879/**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 */83export const createDraft = immer.createDraft.bind(immer)8485/**86 * Finalize an Immer draft from a `createDraft` call, returning the base state87 * (if no changes were made) or a modified copy. The draft must *not* be88 * mutated afterwards.89 *90 * Pass a function as the 2nd argument to generate Immer patches based on the91 * changes that were made.92 */93export const finishDraft = immer.finishDraft.bind(immer)9495/**96 * This function is actually a no-op, but can be used to cast an immutable type97 * to an draft type and make TypeScript happy98 *99 * @param value100 */101export function castDraft<T>(value: T): Draft<T> {102 return value as any103}104105/**106 * This function is actually a no-op, but can be used to cast a mutable type107 * to an immutable type and make TypeScript happy108 * @param value109 */110export function castImmutable<T>(value: T): Immutable<T> {111 return value as any112}113114export {Immer}115116export {enablePatches} from "./plugins/patches"117export {enableMapSet} from "./plugins/mapset"