Skip to content
Myr

immer.ts

This file

Path

src/immer.ts

117 lines · 3.4 KiB · TypeScript

Last changed by

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

History of this file

Take it

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 2why, one step back
2 IProduce,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 3–4why, one step back
3 IProduceWithPatches,
4 Immer,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 5why, one step back
5 Draft,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 6–12why, one step back
6 Immutable
7} from "./internal"89export {10 Draft,11 Immutable,12 Patch,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 13why, one step back
13 PatchListener,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 14why, one step back
14 original,

Immer 7Michel Weststratebefore adoption467ea5d

line 15why, one step back
15 current,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 16why, one step back
16 isDraft,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 17–18why, one step back
17 isDraftable,
18 NOTHING as nothing,

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 19–20why, one step back
19 DRAFTABLE as immerable,
20 freeze

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 21why, one step back
21} from "./internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 22why, one step back
22

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 23why, one step back
23const immer = new Immer()

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 24why, one step back
24

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 25–45why, one step back
25/**
26 * The `produce` function takes a value and a "recipe function" (whose27 * return value often depends on the base state). The recipe function is28 * free to mutate its first argument however it wants. All mutations are29 * only ever applied to a __copy__ of the base state.30 *31 * Pass only a function to create a "curried producer" which relieves you32 * from passing the recipe function every time.33 *34 * Only plain objects and arrays are made mutable. All other objects are35 * considered uncopyable.36 *37 * Note: This function is __bound__ to its `Immer` instance.38 *39 * @param {any} base - the initial state40 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified41 * @param {Function} patchListener - optional function that will be called with all the patches produced here42 * @returns {any} a new state, or the initial state if nothing was modified43 */44export const produce: IProduce = immer.produce45export default produce

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 46why, one step back
46

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 47–53why, one step back
47/**
48 * Like `produce`, but `produceWithPatches` always returns a tuple49 * [nextState, patches, inversePatches] (instead of just the next state)50 */51export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(52 immer53)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 54why, one step back
54

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 55–60why, one step back
55/**
56 * Pass true to automatically freeze all copies created by Immer.57 *58 * By default, auto-freezing is disabled in production.59 */60export const setAutoFreeze = immer.setAutoFreeze.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 61why, one step back
61

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 62–68why, one step back
62/**
63 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is64 * always faster than using ES5 proxies.65 *66 * By default, feature detection is used, so calling this is rarely necessary.67 */68export const setUseProxies = immer.setUseProxies.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 69why, one step back
69

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 70–75why, one step back
70/**
71 * Apply an array of Immer patches to the first argument.72 *73 * This function is a producer, which means copy-on-write is in effect.74 */75export const applyPatches = immer.applyPatches.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 76why, one step back
76

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 77–81why, one step back
77/**
78 * Create an Immer draft from the given base state, which may be a draft itself.79 * The draft can be modified until you finalize it with the `finishDraft` function.80 */81export const createDraft = immer.createDraft.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 82why, one step back
82

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 83–91why, one step back
83/**
84 * Finalize an Immer draft from a `createDraft` call, returning the base state85 * (if no changes were made) or a modified copy. The draft must *not* be86 * mutated afterwards.87 *88 * Pass a function as the 2nd argument to generate Immer patches based on the89 * changes that were made.90 */91export const finishDraft = immer.finishDraft.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 92why, one step back
92

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 93–101why, one step back
93/**
94 * This function is actually a no-op, but can be used to cast an immutable type95 * to an draft type and make TypeScript happy96 *97 * @param value98 */99export function castDraft<T>(value: T): Draft<T> {100 return value as any101}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 102why, one step back
102

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 103–110why, one step back
103/**
104 * This function is actually a no-op, but can be used to cast a mutable type105 * to an immutable type and make TypeScript happy106 * @param value107 */108export function castImmutable<T>(value: T): Immutable<T> {109 return value as any110}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 111why, one step back
111

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 112why, one step back
112export {Immer}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 113why, one step back
113

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 114–117why, one step back
114export {enableES5} from "./plugins/es5"
115export {enablePatches} from "./plugins/patches"116export {enableMapSet} from "./plugins/mapset"117export {enableAllPlugins} from "./plugins/all"