Skip to content
Myr

Basic implementation for `current`

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

authored by Michel Weststrate · committed by Michel Weststrate · parent 0321d34 · Browse files · 8 files+110 added−59 removed
This commit

State

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

Diff stats

8 files+110 added−59 removed

  • tests:+22 added−17 removed
  • other:+88 added−42 removed

8 files+110 added−59 removed

modified:__tests__/current.js+22 added-17 removed
added:src/core/current.ts+38 added
1import {2 die,3 isDraft,4 shallowCopy,5 each,6 DRAFT_STATE,7 get,8 set,9 ImmerState,10 isDraftable11} from "../internal"1213export function current<T>(value: T): T14export function current(value: any): any {15 if (!isDraft(value)) die(22, value)16 return currentImpl(value)17}1819function currentImpl(value: any): any {20 if (!isDraftable(value)) return value21 const state: ImmerState | undefined = value[DRAFT_STATE]22 let copy: any23 if (state) {24 if (!state.modified_) return state.base_25 // Optimization: avoid generating new drafts during copying26 state.finalized_ = true27 copy = shallowCopy(value)28 state.finalized_ = false29 } else {30 copy = shallowCopy(value)31 }3233 each(copy, (key, childValue) => {34 if (state && get(state.base_, key) === childValue) return // no need to copy a current of something that didn't change35 set(copy, key, currentImpl(childValue))36 })37 return copy38}
modified:src/core/finalize.ts+1 added-1 removed
131 unmodified lines
132 rootScope.canAutoFreeze_ = false133 } else return134 }135 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.136 if (isDraftable(childValue) && !Object.isFrozen(childValue)) {136 if (isDraftable(childValue)) {137 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {138 // optimization: if an object is not a draft, and we don't have to139 // deepfreeze everything, and we are sure that no drafts are left in the remaining object140 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.
16 unmodified lines
modified:src/types/index.js.flow+36 added-34 removed
1// @flow23export interface Patch {4 op: "replace" | "remove" | "add",5 path: (string|number)[],6 value?: any4 op: "replace" | "remove" | "add";5 path: (string | number)[];6 value?: any;7}89export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void1011type Base = {...} | Array<any> | Map<any> | Set<any>;11type Base = {...} | Array<any> | Map<any> | Set<any>12interface IProduce {13 /**14 * Immer takes a state, and runs a function against it.15 * That function can freely mutate the state, as it will create copies-on-write.16 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.17 *18 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe19 * any time it is called with the current state.20 *21 * @param currentState - the state to start with22 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified23 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined24 * @returns The next state: a new state, or the current state if nothing was modified25 */26 <S: Base>(27 currentState: S,28 recipe: (draftState: S) => S | void,29 patchListener?: PatchListener30 ): S;31 // curried invocations with inital state32 <S: Base, A = void, B = void, C = void>(33 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,34 initialState: S35 ): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => S;36 // curried invocations without inital state37 <S: Base, A = void, B = void, C = void>(38 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void39 ): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S;13 /**14 * Immer takes a state, and runs a function against it.15 * That function can freely mutate the state, as it will create copies-on-write.16 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.17 *18 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe19 * any time it is called with the current state.20 *21 * @param currentState - the state to start with22 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified23 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined24 * @returns The next state: a new state, or the current state if nothing was modified25 */26 <S: Base>(27 currentState: S,28 recipe: (draftState: S) => S | void,29 patchListener?: PatchListener30 ): S;31 // curried invocations with inital state32 <S: Base, A = void, B = void, C = void>(33 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,34 initialState: S35 ): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => S;36 // curried invocations without inital state37 <S: Base, A = void, B = void, C = void>(38 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void39 ): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S;40}4142declare export var produce: IProduce43declare export default IProduce
17 unmodified lines
61declare export function setUseProxies(useProxies: boolean): void6263declare export function applyPatches<S>(state: S, patches: Patch[]): S6465declare export function original<S>(value: S): ?S65declare export function original<S>(value: S): S6667declare export function current<S>(value: S): S6869declare export function isDraft(value: any): boolean7071/**70 * Creates a mutable draft from an (immutable) object / array. 72 * Creates a mutable draft from an (immutable) object / array.73 * The draft can be modified until `finishDraft` is called74 */75declare export function createDraft<T>(base: T): T7677/**76 * Given a draft that was created using `createDraft`, 78 * Given a draft that was created using `createDraft`,79 * finalizes the draft into a new immutable object.80 * Optionally a patch-listener can be provided to gather the patches that are needed to construct the object.81 */82declare export function finishDraft<T>(base: T, listener?: PatchListener): T
5 unmodified lines
modified:src/utils/common.ts+8 added-7 removed
25 unmodified lines
26/*#__PURE__*/27export function isDraftable(value: any): boolean {28 if (!value) return false29 return (30 isPlainObject(value) ||31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)30 (isPlainObject(value) ||31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)) &&36 !isFrozen(value)37 )38}3940/*#__PURE__*/
130 unmodified lines
171 return Object.create(Object.getPrototypeOf(base), descriptors)172}173174export function freeze(obj: any, deep: boolean): void {174 if (isDraft(obj) || isFrozen(obj) || !isDraftable(obj)) return175 if (isDraft(obj) || !isDraftable(obj)) return176 if (getArchtype(obj) > 1 /* Map or Set */) {177 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any178 }179 Object.freeze(obj)
12 unmodified lines
modified:src/utils/errors.ts+3 added
31 unmodified lines
32 19: "plugin not loaded",33 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",34 21(thing: string) {35 return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`36 },37 22(thing: string) {38 return `'current' expects a draft, got: ${thing}`39 }40} as const4142export function die(error: keyof typeof errors, ...args: any[]): never {
15 unmodified lines
modified:src/immer.ts+1 added
10 unmodified lines
11 Immutable,12 Patch,13 PatchListener,14 original,15 current,16 isDraft,17 isDraftable,18 NOTHING as nothing,19 DRAFTABLE as immerable
97 unmodified lines
modified:src/internal.ts+1 added
6 unmodified lines
7export * from "./core/scope"8export * from "./core/finalize"9export * from "./core/proxy"10export * from "./core/immerClass"11export * from "./core/current"