immer.ts
This file
Path
src/immer.ts
114 lines · 3.3 KiB · TypeScript
Last changed by
TSDXMichel Weststratebefore adoption4c4fecc
History of this fileTake it
feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 2why, one step back2 IProduce,More WIPMichel Weststratebefore adoptionee1c977
lines 3–4why, one step back3 IProduceWithPatches,4 Immer,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 5why, one step back5 Draft,More WIPMichel Weststratebefore adoptionee1c977
lines 6–12why, one step back6 Immutable7} from "./internal"89export {10 Draft,11 Immutable,12 Patch,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 13why, one step back13 PatchListener,More WIPMichel Weststratebefore adoptionee1c977
line 14why, one step back14 original,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 15why, one step back15 isDraft,More WIPMichel Weststratebefore adoptionee1c977
lines 16–17why, one step back16 isDraftable,17 NOTHING as nothing,TSDXMichel Weststratebefore adoption4c4fecc
line 18why, one step back18 DRAFTABLE as immerablefix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 19why, one step back19} from "./internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 20why, one step back20More WIPMichel Weststratebefore adoptionee1c977
line 21why, one step back21const immer = new Immer()feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 22why, one step back22More WIPMichel Weststratebefore adoptionee1c977
lines 23–43why, one step back23/**24 * The `produce` function takes a value and a "recipe function" (whose25 * return value often depends on the base state). The recipe function is26 * free to mutate its first argument however it wants. All mutations are27 * only ever applied to a __copy__ of the base state.28 *29 * Pass only a function to create a "curried producer" which relieves you30 * from passing the recipe function every time.31 *32 * Only plain objects and arrays are made mutable. All other objects are33 * considered uncopyable.34 *35 * Note: This function is __bound__ to its `Immer` instance.36 *37 * @param {any} base - the initial state38 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified39 * @param {Function} patchListener - optional function that will be called with all the patches produced here40 * @returns {any} a new state, or the initial state if nothing was modified41 */42export const produce: IProduce = immer.produce43export default producefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 44why, one step back44More WIPMichel Weststratebefore adoptionee1c977
lines 45–51why, one step back45/**46 * Like `produce`, but `produceWithPatches` always returns a tuple47 * [nextState, patches, inversePatches] (instead of just the next state)48 */49export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(50 immer51)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 52why, one step back52More WIPMichel Weststratebefore adoptionee1c977
lines 53–58why, one step back53/**54 * Pass true to automatically freeze all copies created by Immer.55 *56 * By default, auto-freezing is disabled in production.57 */58export const setAutoFreeze = immer.setAutoFreeze.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 59why, one step back59More WIPMichel Weststratebefore adoptionee1c977
lines 60–66why, one step back60/**61 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is62 * always faster than using ES5 proxies.63 *64 * By default, feature detection is used, so calling this is rarely necessary.65 */66export const setUseProxies = immer.setUseProxies.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 67why, one step back67More WIPMichel Weststratebefore adoptionee1c977
lines 68–73why, one step back68/**69 * Apply an array of Immer patches to the first argument.70 *71 * This function is a producer, which means copy-on-write is in effect.72 */73export const applyPatches = immer.applyPatches.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 74why, one step back74More WIPMichel Weststratebefore adoptionee1c977
lines 75–79why, one step back75/**76 * Create an Immer draft from the given base state, which may be a draft itself.77 * The draft can be modified until you finalize it with the `finishDraft` function.78 */79export const createDraft = immer.createDraft.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 80why, one step back80More WIPMichel Weststratebefore adoptionee1c977
lines 81–89why, one step back81/**82 * Finalize an Immer draft from a `createDraft` call, returning the base state83 * (if no changes were made) or a modified copy. The draft must *not* be84 * mutated afterwards.85 *86 * Pass a function as the 2nd argument to generate Immer patches based on the87 * changes that were made.88 */89export const finishDraft = immer.finishDraft.bind(immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 90why, one step back90More WIPMichel Weststratebefore adoptionee1c977
lines 91–98why, one step back91/**92 * This function is actually a no-op, but can be used to cast an immutable type93 * to an draft type and make TypeScript happy94 *95 * @param value96 */97export function castDraft<T>(value: T): Draft<T> {98 return value as anySmall code cleanupMichel Weststratebefore adoption9217809
line 99why, one step back99}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 100why, one step back100More WIPMichel Weststratebefore adoptionee1c977
lines 101–107why, one step back101/**102 * This function is actually a no-op, but can be used to cast a mutable type103 * to an immutable type and make TypeScript happy104 * @param value105 */106export function castImmutable<T>(value: T): Immutable<T> {107 return value as anySmall code cleanupMichel Weststratebefore adoption9217809
line 108why, one step back108}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 109why, one step back109More WIPMichel Weststratebefore adoptionee1c977
line 110why, one step back110export {Immer}TSDXMichel Weststratebefore adoption4c4fecc
lines 111–114why, one step back111112export {enableES5} from "./plugins/es5"113export {enablePatches} from "./plugins/patches"114export {enableMapSet} from "./plugins/mapset"