Skip to content
Myr

feat: add hooks to the `Immer` class

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Hooks are passed as options to the Immer class. onAssign(state, prop, value) Called for every property assigned to by a producer onDelete(state, prop) Called for every property deleted by a producer onCopy(state) Called whenever a copy becomes finalized These hooks are passed an `ImmerState` object. (defined at the end of "src/index.d.ts") The `state` is guaranteed to be both modified and finalized if ever passed to a hook.

authored by aleclarson · committed by aleclarson · parent f36d329 · Browse files · 2 files+51 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−14 removed

  • other:+51 added−14 removed

2 files+51 added−14 removed

modified:src/immer.js+37 added-13 removed
modified:src/index.d.ts+14 added-1 removed
128 unmodified lines
129130export function isDraft(value: any): boolean131132export class Immer {133 constructor(config: {useProxies?: boolean; autoFreeze?: boolean})133 constructor(config: {134 useProxies?: boolean135 autoFreeze?: boolean136 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void137 onDelete?: (state: ImmerState, prop: keyof any) => void138 onCopy?: (state: ImmerState) => void139 })140 /**141 * The `produce` function takes a value and a "recipe function" (whose142 * return value often depends on the base state). The recipe function is143 * free to mutate its first argument however it wants. All mutations are
34 unmodified lines
178 * By default, feature detection is used, so calling this is rarely necessary.179 */180 setUseProxies(useProxies: boolean): void181}182183export interface ImmerState<T = any> {184 parent?: ImmerState185 base: T186 copy: T187 assigned: {[prop: string]: boolean}188}