current.ts
This file
Path
src/core/current.ts
46 lines · 1.2 KiB · TypeScript
Last changed by
feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
History of this fileTake it
The walk stopped after 200 commits: lines older than 3f9a94e point at it.
chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1–9why, one step back1import {2 die,3 isDraft,4 shallowCopy,5 each,6 DRAFT_STATE,7 set,8 ImmerState,9 isDraftable,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 10why, one step back10 isFrozenchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 11–15why, one step back11} from "../internal"1213/** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */14export function current<T>(value: T): T15export function current(value: any): any {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 16why, one step back16 if (!isDraft(value)) die(10, value)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 17–20why, one step back17 return currentImpl(value)18}1920function currentImpl(value: any): any {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 21why, one step back21 if (!isDraftable(value) || isFrozen(value)) return valuechore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 22–23why, one step back22 const state: ImmerState | undefined = value[DRAFT_STATE]23 let copy: anyfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 24why, one step back24 let strict = true // Default to strict for compatibilitychore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 25why, one step back25 if (state) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 26why, one step back26 if (!state.modified_) return state.base_chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 27–28why, one step back27 // Optimization: avoid generating new drafts during copying28 state.finalized_ = trueMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 29why, one step back29 copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_)feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 30why, one step back30 strict = state.scope_.immer_.shouldUseStrictIteration()chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 31why, one step back31 } else {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 32why, one step back32 copy = shallowCopy(value, true)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 33why, one step back33 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 34why, one step back34 // recursefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 35–41why, one step back35 each(36 copy,37 (key, childValue) => {38 set(copy, key, currentImpl(childValue))39 },40 strict41 )Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 42–43why, one step back42 if (state) {43 state.finalized_ = falsechore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 44why, one step back44 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 45why, one step back45 return copychore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 46why, one step back46}