Skip to content
Myr

index.js

This file

Path

src/index.js

79 lines · 2.6 KiB · JavaScript

Last changed by

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

History of this file

Take it

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–27why, one step back
1import {Immer} from "./immer"
23const immer = new Immer()45/**6 * The `produce` function takes a value and a "recipe function" (whose7 * return value often depends on the base state). The recipe function is8 * free to mutate its first argument however it wants. All mutations are9 * only ever applied to a __copy__ of the base state.10 *11 * Pass only a function to create a "curried producer" which relieves you12 * from passing the recipe function every time.13 *14 * Only plain objects and arrays are made mutable. All other objects are15 * considered uncopyable.16 *17 * Note: This function is __bound__ to its `Immer` instance.18 *19 * @param {any} base - the initial state20 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified21 * @param {Function} patchListener - optional function that will be called with all the patches produced here22 * @returns {any} a new state, or the initial state if nothing was modified23 */24export const produce = immer.produce25export default produce2627/**

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

lines 28–33why, one step back
28 * Like `produce`, but `produceWithPatches` always returns a tuple
29 * [nextState, patches, inversePatches] (instead of just the next state)30 */31export const produceWithPatches = immer.produceWithPatches.bind(immer)3233/**

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 34–37why, one step back
34 * Pass true to automatically freeze all copies created by Immer.
35 *36 * By default, auto-freezing is disabled in production.37 */

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

line 38why, one step back
38export const setAutoFreeze = immer.setAutoFreeze.bind(immer)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 39–45why, one step back
39
40/**41 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is42 * always faster than using ES5 proxies.43 *44 * By default, feature detection is used, so calling this is rarely necessary.45 */

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

line 46why, one step back
46export const setUseProxies = immer.setUseProxies.bind(immer)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 47–52why, one step back
47
48/**49 * Apply an array of Immer patches to the first argument.50 *51 * This function is a producer, which means copy-on-write is in effect.52 */

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

line 53why, one step back
53export const applyPatches = immer.applyPatches.bind(immer)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 54why, one step back
54

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 55–70why, one step back
55/**
56 * Create an Immer draft from the given base state, which may be a draft itself.57 * The draft can be modified until you finalize it with the `finishDraft` function.58 */59export const createDraft = immer.createDraft.bind(immer)6061/**62 * Finalize an Immer draft from a `createDraft` call, returning the base state63 * (if no changes were made) or a modified copy. The draft must *not* be64 * mutated afterwards.65 *66 * Pass a function as the 2nd argument to generate Immer patches based on the67 * changes that were made.68 */69export const finishDraft = immer.finishDraft.bind(immer)70

feat: limited class supportaleclarsonbefore adoption9877d64

line 71why, one step back
71export {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 72–76why, one step back
72 original,
73 isDraft,74 isDraftable,75 NOTHING as nothing,76 DRAFTABLE as immerable

feat: limited class supportaleclarsonbefore adoption9877d64

line 77why, one step back
77} from "./common"

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 78–79why, one step back
78
79export {Immer}