Skip to content
Myr

immer.ts

This file

Path

src/immer.ts

117 lines · 3.4 KiB · TypeScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 949960f point at it.

fix: fixed broken minimized error url (issue#550)Michel Weststratebefore adoption949960f

lines 1–14why, one step back
1import {
2 IProduce,3 IProduceWithPatches,4 Immer,5 Draft,6 Immutable7} from "./internal"89export {10 Draft,11 Immutable,12 Patch,13 PatchListener,14 original,

Immer 7Michel Weststratebefore adoption467ea5d

line 15why, one step back
15 current,

fix: fixed broken minimized error url (issue#550)Michel Weststratebefore adoption949960f

lines 16–18why, one step back
16 isDraft,
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: fixed broken minimized error url (issue#550)Michel Weststratebefore adoption949960f

lines 21–57why, one step back
21} from "./internal"
2223const immer = new Immer()2425/**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 produce4647/**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)5455/**56 * Pass true to automatically freeze all copies created by Immer.57 *

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

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

fix: fixed broken minimized error url (issue#550)Michel Weststratebefore adoption949960f

lines 59–117why, one step back
59 */
60export const setAutoFreeze = immer.setAutoFreeze.bind(immer)6162/**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)6970/**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)7677/**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)8283/**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)9293/**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}102103/**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}111112export {Immer}113114export {enableES5} from "./plugins/es5"115export {enablePatches} from "./plugins/patches"116export {enableMapSet} from "./plugins/mapset"117export {enableAllPlugins} from "./plugins/all"