Skip to content
Myr

immer.ts

This file

Path

src/immer.ts

115 lines · 3.3 KiB · TypeScript

Last changed by

New build set. Current statsMichel Weststratebefore adoption9aec0fe

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,

More WIPMichel Weststratebefore adoptionee1c977

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,

More WIPMichel Weststratebefore adoptionee1c977

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,

More WIPMichel Weststratebefore adoptionee1c977

line 14why, one step back
14 original,

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

line 15why, one step back
15 isDraft,

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

line 18why, one step back
18 DRAFTABLE as immerable

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 20why, one step back
20

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 22why, one step back
22

More WIPMichel Weststratebefore adoptionee1c977

lines 23–43why, one step back
23/**
24 * The `produce` function takes a value and a "recipe function" (whose25 * return value often depends on the base state). The recipe function is26 * free to mutate its first argument however it wants. All mutations are27 * only ever applied to a __copy__ of the base state.28 *29 * Pass only a function to create a "curried producer" which relieves you30 * from passing the recipe function every time.31 *32 * Only plain objects and arrays are made mutable. All other objects are33 * considered uncopyable.34 *35 * Note: This function is __bound__ to its `Immer` instance.36 *37 * @param {any} base - the initial state38 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified39 * @param {Function} patchListener - optional function that will be called with all the patches produced here40 * @returns {any} a new state, or the initial state if nothing was modified41 */42export const produce: IProduce = immer.produce43export default produce

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 44why, one step back
44

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 52why, one step back
52

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 59why, one step back
59

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 67why, one step back
67

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 74why, one step back
74

More WIPMichel Weststratebefore adoptionee1c977

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 80why, one step back
80

More WIPMichel Weststratebefore adoptionee1c977

lines 81–89why, one step back
81/**
82 * Finalize an Immer draft from a `createDraft` call, returning the base state83 * (if no changes were made) or a modified copy. The draft must *not* be84 * mutated afterwards.85 *86 * Pass a function as the 2nd argument to generate Immer patches based on the87 * changes that were made.88 */89export const finishDraft = immer.finishDraft.bind(immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 90why, one step back
90

More WIPMichel Weststratebefore adoptionee1c977

lines 91–98why, one step back
91/**
92 * This function is actually a no-op, but can be used to cast an immutable type93 * to an draft type and make TypeScript happy94 *95 * @param value96 */97export function castDraft<T>(value: T): Draft<T> {98 return value as any

Small code cleanupMichel Weststratebefore adoption9217809

line 99why, one step back
99}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 100why, one step back
100

More WIPMichel Weststratebefore adoptionee1c977

lines 101–107why, one step back
101/**
102 * This function is actually a no-op, but can be used to cast a mutable type103 * to an immutable type and make TypeScript happy104 * @param value105 */106export function castImmutable<T>(value: T): Immutable<T> {107 return value as any

Small code cleanupMichel Weststratebefore adoption9217809

line 108why, one step back
108}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 109why, one step back
109

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 111–114why, one step back
111
112export {enableES5} from "./plugins/es5"113export {enablePatches} from "./plugins/patches"114export {enableMapSet} from "./plugins/mapset"

New build set. Current statsMichel Weststratebefore adoption9aec0fe

line 115why, one step back
115export {enableAllPlugins} from "./plugins/all"