index.ts
This file
Path
src/index.ts
84 lines · 2.7 KiB · TypeScript
Last changed by
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 1–84why, one step back1import {Immer} from "./immer"2import {IProduce, IProduceWithPatches} from "./types"34export {Draft, Immutable, Patch, PatchListener} from "./types"56const immer = new Immer()78/**9 * The `produce` function takes a value and a "recipe function" (whose10 * return value often depends on the base state). The recipe function is11 * free to mutate its first argument however it wants. All mutations are12 * only ever applied to a __copy__ of the base state.13 *14 * Pass only a function to create a "curried producer" which relieves you15 * from passing the recipe function every time.16 *17 * Only plain objects and arrays are made mutable. All other objects are18 * considered uncopyable.19 *20 * Note: This function is __bound__ to its `Immer` instance.21 *22 * @param {any} base - the initial state23 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified24 * @param {Function} patchListener - optional function that will be called with all the patches produced here25 * @returns {any} a new state, or the initial state if nothing was modified26 */27export const produce: IProduce = immer.produce28export default produce2930/**31 * Like `produce`, but `produceWithPatches` always returns a tuple32 * [nextState, patches, inversePatches] (instead of just the next state)33 */34export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(35 immer36)3738/**39 * Pass true to automatically freeze all copies created by Immer.40 *41 * By default, auto-freezing is disabled in production.42 */43export const setAutoFreeze = immer.setAutoFreeze.bind(immer)4445/**46 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is47 * always faster than using ES5 proxies.48 *49 * By default, feature detection is used, so calling this is rarely necessary.50 */51export const setUseProxies = immer.setUseProxies.bind(immer)5253/**54 * Apply an array of Immer patches to the first argument.55 *56 * This function is a producer, which means copy-on-write is in effect.57 */58export const applyPatches = immer.applyPatches.bind(immer)5960/**61 * Create an Immer draft from the given base state, which may be a draft itself.62 * The draft can be modified until you finalize it with the `finishDraft` function.63 */64export const createDraft = immer.createDraft.bind(immer)6566/**67 * Finalize an Immer draft from a `createDraft` call, returning the base state68 * (if no changes were made) or a modified copy. The draft must *not* be69 * mutated afterwards.70 *71 * Pass a function as the 2nd argument to generate Immer patches based on the72 * changes that were made.73 */74export const finishDraft = immer.finishDraft.bind(immer)7576export {77 original,78 isDraft,79 isDraftable,80 NOTHING as nothing,81 DRAFTABLE as immerable82} from "./common"8384export {Immer}