index.js
This file
Path
src/index.js
73 lines · 2.4 KiB · JavaScript
Last changed by
style: use tabsuse-tabsbefore adoptiona9c0fd0
History of this fileTake it
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–31why, one step back1import {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/**28 * Pass true to automatically freeze all copies created by Immer.29 *30 * By default, auto-freezing is disabled in production.31 */fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 32why, one step back32export const setAutoFreeze = immer.setAutoFreeze.bind(immer)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 33–39why, one step back3334/**35 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is36 * always faster than using ES5 proxies.37 *38 * By default, feature detection is used, so calling this is rarely necessary.39 */fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 40why, one step back40export const setUseProxies = immer.setUseProxies.bind(immer)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 41–46why, one step back4142/**43 * Apply an array of Immer patches to the first argument.44 *45 * This function is a producer, which means copy-on-write is in effect.46 */fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 47why, one step back47export const applyPatches = immer.applyPatches.bind(immer)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 48why, one step back48Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 49–64why, one step back49/**50 * Create an Immer draft from the given base state, which may be a draft itself.51 * The draft can be modified until you finalize it with the `finishDraft` function.52 */53export const createDraft = immer.createDraft.bind(immer)5455/**56 * Finalize an Immer draft from a `createDraft` call, returning the base state57 * (if no changes were made) or a modified copy. The draft must *not* be58 * mutated afterwards.59 *60 * Pass a function as the 2nd argument to generate Immer patches based on the61 * changes that were made.62 */63export const finishDraft = immer.finishDraft.bind(immer)64feat: limited class supportaleclarsonbefore adoption9877d64
line 65why, one step back65export {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 66–70why, one step back66 original,67 isDraft,68 isDraftable,69 NOTHING as nothing,70 DRAFTABLE as immerablefeat: limited class supportaleclarsonbefore adoption9877d64
line 71why, one step back71} from "./common"feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 72–73why, one step back7273export {Immer}