Skip to content
Myr

index.js

This file

Path

src/index.js

52 lines · 1.8 KiB · JavaScript

Last changed by

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

History of this file

Take it

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–52why, one step back
1import {applyPatches as applyPatchesImpl} from "./patches"
2import {Immer} from "./immer"34const immer = new Immer()56/**7 * The `produce` function takes a value and a "recipe function" (whose8 * return value often depends on the base state). The recipe function is9 * free to mutate its first argument however it wants. All mutations are10 * only ever applied to a __copy__ of the base state.11 *12 * Pass only a function to create a "curried producer" which relieves you13 * from passing the recipe function every time.14 *15 * Only plain objects and arrays are made mutable. All other objects are16 * considered uncopyable.17 *18 * Note: This function is __bound__ to its `Immer` instance.19 *20 * @param {any} base - the initial state21 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified22 * @param {Function} patchListener - optional function that will be called with all the patches produced here23 * @returns {any} a new state, or the initial state if nothing was modified24 */25export const produce = immer.produce26export default produce2728/**29 * Pass true to automatically freeze all copies created by Immer.30 *31 * By default, auto-freezing is disabled in production.32 */33export const setAutoFreeze = value => immer.setAutoFreeze(value)3435/**36 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is37 * always faster than using ES5 proxies.38 *39 * By default, feature detection is used, so calling this is rarely necessary.40 */41export const setUseProxies = value => immer.setUseProxies(value)4243/**44 * Apply an array of Immer patches to the first argument.45 *46 * This function is a producer, which means copy-on-write is in effect.47 */48export const applyPatches = produce(applyPatchesImpl)4950export {original, isDraft, NOTHING as nothing} from "./common"5152export {Immer}