Skip to content
Myr

current.ts

This file

Path

src/core/current.ts

40 lines · 1.1 KiB · TypeScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than bc35916 point at it.

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 22–24why, one step back
22 const state: ImmerState | undefined = value[DRAFT_STATE]
23 let copy: any24 if (state) {

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 29why, one step back
29 } else {

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 31why, one step back
31 }

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

line 32why, one step back
32 // recurse

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 33–35why, one step back
33 each(copy, (key, childValue) => {
34 set(copy, key, currentImpl(childValue))35 })

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

lines 36–37why, one step back
36 if (state) {
37 state.finalized_ = false

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 38why, one step back
38 }

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

line 39why, one step back
39 return copy

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 40why, one step back
40}