immer.ts
This file
Path
src/immer.ts
114 lines · 3.2 KiB · TypeScript
Last changed by
fix: export `Objectish` type (#1043)Milan Jaritzbefore adoption75e004d
History of this fileTake it
The walk stopped after 200 commits: lines older than f97d037 point at it.
docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 1–18why, one step back1import {2 IProduce,3 IProduceWithPatches,4 Immer,5 Draft,6 Immutable7} from "./internal"89export {10 Draft,11 Immutable,12 Patch,13 PatchListener,14 original,15 current,16 isDraft,17 isDraftable,18 NOTHING as nothing,feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
line 19why, one step back19 DRAFTABLE as immerable,fix: export `Objectish` type (#1043)Milan Jaritzbefore adoption75e004d
lines 20–21why, one step back20 freeze,21 Objectishdocs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 22–57why, one step back22} from "./internal"2324const immer = new Immer()2526/**27 * The `produce` function takes a value and a "recipe function" (whose28 * return value often depends on the base state). The recipe function is29 * free to mutate its first argument however it wants. All mutations are30 * only ever applied to a __copy__ of the base state.31 *32 * Pass only a function to create a "curried producer" which relieves you33 * from passing the recipe function every time.34 *35 * Only plain objects and arrays are made mutable. All other objects are36 * considered uncopyable.37 *38 * Note: This function is __bound__ to its `Immer` instance.39 *40 * @param {any} base - the initial state41 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified42 * @param {Function} patchListener - optional function that will be called with all the patches produced here43 * @returns {any} a new state, or the initial state if nothing was modified44 */45export const produce: IProduce = immer.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 back58 * Always freeze by default, even in production modedocs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 59–62why, one step back59 */60export const setAutoFreeze = immer.setAutoFreeze.bind(immer)6162/**Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 63why, one step back63 * Pass true to enable strict shallow copy.docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
line 64why, one step back64 *Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 65why, one step back65 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
line 66why, one step back66 */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 67why, one step back67export const setUseStrictShallowCopy = immer.setUseStrictShallowCopy.bind(immer)docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 68–114why, one step back6869/**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)7576/**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)8182/**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)9192/**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}101102/**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}110111export {Immer}112113export {enablePatches} from "./plugins/patches"114export {enableMapSet} from "./plugins/mapset"