Skip to content
Myr

current.ts

This file

Path

src/core/current.ts

60 lines · 1.7 KiB · TypeScript

Last changed by

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

History of this file

Take it

Immer 7Michel Weststratebefore adoption467ea5d

lines 1–10why, one step back
1import {
2 die,3 isDraft,4 shallowCopy,5 each,6 DRAFT_STATE,7 get,8 set,9 ImmerState,10 isDraftable,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 11why, one step back
11 Archtype,

Immer 7Michel Weststratebefore adoption467ea5d

lines 12–15why, one step back
12 getArchtype,
13 getPlugin14} from "../internal"15

fix: Add missing type `current` for pre-TS 3.7 typesMichel Weststratebefore adoption7d6b57b

line 16why, one step back
16/** 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. */

Immer 7Michel Weststratebefore adoption467ea5d

lines 17–46why, one step back
17export function current<T>(value: T): T
18export function current(value: any): any {19 if (!isDraft(value)) die(22, value)20 return currentImpl(value)21}2223function currentImpl(value: any): any {24 if (!isDraftable(value)) return value25 const state: ImmerState | undefined = value[DRAFT_STATE]26 let copy: any27 const archType = getArchtype(value)28 if (state) {29 if (30 !state.modified_ &&31 (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state as any))32 )33 return state.base_34 // Optimization: avoid generating new drafts during copying35 state.finalized_ = true36 copy = copyHelper(value, archType)37 state.finalized_ = false38 } else {39 copy = copyHelper(value, archType)40 }4142 each(copy, (key, childValue) => {43 if (state && get(state.base_, key) === childValue) return // no need to copy or search in something that didn't change44 set(copy, key, currentImpl(childValue))45 })46 // In the future, we might consider freezing here, based on the current settings

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 47why, one step back
47 return archType === Archtype.Set ? new Set(copy) : copy

Immer 7Michel Weststratebefore adoption467ea5d

lines 48–52why, one step back
48}
4950function copyHelper(value: any, archType: number): any {51 // creates a shallow copy, even if it is a map or set52 switch (archType) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 53why, one step back
53 case Archtype.Map:

Immer 7Michel Weststratebefore adoption467ea5d

line 54why, one step back
54 return new Map(value)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 55why, one step back
55 case Archtype.Set:

Immer 7Michel Weststratebefore adoption467ea5d

lines 56–60why, one step back
56 // Set will be cloned as array temporarily, so that we can replace individual items
57 return Array.from(value)58 }59 return shallowCopy(value)60}