Skip to content
Myr

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 file

Take 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 back
1import {
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 back
10 isFrozen

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 11–15why, one step back
11} 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 back
16 if (!isDraft(value)) die(10, value)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 17–20why, one step back
17 return currentImpl(value)
18}1920function currentImpl(value: any): any {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 21why, one step back
21 if (!isDraftable(value) || isFrozen(value)) return value

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 22–23why, one step back
22 const state: ImmerState | undefined = value[DRAFT_STATE]
23 let copy: any

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 24why, one step back
24 let strict = true // Default to strict for compatibility

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 25why, one step back
25 if (state) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 26why, one step back
26 if (!state.modified_) return state.base_

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

lines 27–28why, one step back
27 // Optimization: avoid generating new drafts during copying
28 state.finalized_ = true

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 29why, one step back
29 copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_)

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 30why, one step back
30 strict = state.scope_.immer_.shouldUseStrictIteration()

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 31why, one step back
31 } else {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 32why, one step back
32 copy = shallowCopy(value, true)

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 33why, one step back
33 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 34why, one step back
34 // recurse

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 35–41why, one step back
35 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 back
42 if (state) {
43 state.finalized_ = false

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 44why, one step back
44 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 45why, one step back
45 return copy

chore: let's test before publishMichel Weststratebefore adoption3f9a94e

line 46why, one step back
46}