immer.ts
This file
Path
src/immer.ts
116 lines · 3.4 KiB · TypeScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 2why, one step back2 IProduce,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 3–4why, one step back3 IProduceWithPatches,4 Immer,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 5why, one step back5 Draft,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 6–12why, one step back6 Immutable7} 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 back13 PatchListener,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 14why, one step back14 original,Immer 7Michel Weststratebefore adoption467ea5d
line 15why, one step back15 current,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 16why, one step back16 isDraft,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 17–19why, one step back17 isDraftable,18 NOTHING as nothing,19 DRAFTABLE as immerablefix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 20why, one step back20} from "./internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 21why, one step back21Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 22why, one step back22const immer = new Immer()feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 23why, one step back23Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 24–44why, one step back24/**25 * The `produce` function takes a value and a "recipe function" (whose26 * return value often depends on the base state). The recipe function is27 * free to mutate its first argument however it wants. All mutations are28 * only ever applied to a __copy__ of the base state.29 *30 * Pass only a function to create a "curried producer" which relieves you31 * from passing the recipe function every time.32 *33 * Only plain objects and arrays are made mutable. All other objects are34 * considered uncopyable.35 *36 * Note: This function is __bound__ to its `Immer` instance.37 *38 * @param {any} base - the initial state39 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified40 * @param {Function} patchListener - optional function that will be called with all the patches produced here41 * @returns {any} a new state, or the initial state if nothing was modified42 */43export const produce: IProduce = immer.produce44export default producefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 45why, one step back45Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 46–52why, one step back46/**47 * Like `produce`, but `produceWithPatches` always returns a tuple48 * [nextState, patches, inversePatches] (instead of just the next state)49 */50export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(51 immer52)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 53why, one step back53Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 54–59why, one step back54/**55 * Pass true to automatically freeze all copies created by Immer.56 *57 * By default, auto-freezing is disabled in production.58 */59export const setAutoFreeze = immer.setAutoFreeze.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 60why, one step back60Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 61–67why, one step back61/**62 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is63 * always faster than using ES5 proxies.64 *65 * By default, feature detection is used, so calling this is rarely necessary.66 */67export const setUseProxies = immer.setUseProxies.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 68why, one step back68Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 69–74why, one step back69/**70 * Apply an array of Immer patches to the first argument.71 *72 * This function is a producer, which means copy-on-write is in effect.73 */74export const applyPatches = immer.applyPatches.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 75why, one step back75Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 76–80why, one step back76/**77 * Create an Immer draft from the given base state, which may be a draft itself.78 * The draft can be modified until you finalize it with the `finishDraft` function.79 */80export const createDraft = immer.createDraft.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 81why, one step back81Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 82–90why, one step back82/**83 * Finalize an Immer draft from a `createDraft` call, returning the base state84 * (if no changes were made) or a modified copy. The draft must *not* be85 * mutated afterwards.86 *87 * Pass a function as the 2nd argument to generate Immer patches based on the88 * changes that were made.89 */90export const finishDraft = immer.finishDraft.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 91why, one step back91Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 92–100why, one step back92/**93 * This function is actually a no-op, but can be used to cast an immutable type94 * to an draft type and make TypeScript happy95 *96 * @param value97 */98export function castDraft<T>(value: T): Draft<T> {99 return value as any100}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 101why, one step back101Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 102–109why, one step back102/**103 * This function is actually a no-op, but can be used to cast a mutable type104 * to an immutable type and make TypeScript happy105 * @param value106 */107export function castImmutable<T>(value: T): Immutable<T> {108 return value as any109}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 110why, one step back110Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 111why, one step back111export {Immer}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 112why, one step back112Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 113–116why, one step back113export {enableES5} from "./plugins/es5"114export {enablePatches} from "./plugins/patches"115export {enableMapSet} from "./plugins/mapset"116export {enableAllPlugins} from "./plugins/all"