Skip to content
Myr

current.ts

This file

Path

src/core/current.ts

59 lines · 1.5 KiB · TypeScript

Last changed by

Made ES5 version workMichel Weststratebefore adoption870f44e

History of this file

Take it

Basic implementation for `current`Michel Weststratebefore adoption7322e82

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

Further current implMichel Weststratebefore adoption5036e7f

lines 10–13why, one step back
10 isDraftable,
11 ArchtypeMap,12 ArchtypeSet,13 getArchtype

Basic implementation for `current`Michel Weststratebefore adoption7322e82

line 14why, one step back
14} from "../internal"

Made ES5 version workMichel Weststratebefore adoption870f44e

line 15why, one step back
15import {getPlugin} from "../utils/plugins"

Basic implementation for `current`Michel Weststratebefore adoption7322e82

lines 16–26why, one step back
16
17export function current<T>(value: T): T18export 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: any

Further current implMichel Weststratebefore adoption5036e7f

line 27why, one step back
27 const archType = getArchtype(value)

Basic implementation for `current`Michel Weststratebefore adoption7322e82

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

Made ES5 version workMichel Weststratebefore adoption870f44e

lines 29–33why, one step back
29 if (
30 !state.modified_ &&31 (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state as any))32 )33 return state.base_

Basic implementation for `current`Michel Weststratebefore adoption7322e82

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

Further current implMichel Weststratebefore adoption5036e7f

line 36why, one step back
36 copy = copyHelper(value, archType)

Basic implementation for `current`Michel Weststratebefore adoption7322e82

lines 37–38why, one step back
37 state.finalized_ = false
38 } else {

Further current implMichel Weststratebefore adoption5036e7f

line 39why, one step back
39 copy = copyHelper(value, archType)

Basic implementation for `current`Michel Weststratebefore adoption7322e82

lines 40–42why, one step back
40 }
4142 each(copy, (key, childValue) => {

Further current implMichel Weststratebefore adoption5036e7f

line 43why, one step back
43 if (state && get(state.base_, key) === childValue) return // no need to copy or search in something that didn't change

Basic implementation for `current`Michel Weststratebefore adoption7322e82

lines 44–45why, one step back
44 set(copy, key, currentImpl(childValue))
45 })

Further current implMichel Weststratebefore adoption5036e7f

lines 46–58why, one step back
46 return archType === ArchtypeSet ? new Set(copy) : copy
47}4849function copyHelper(value: any, archType: number): any {50 // creates a shallow copy, even if it is a map or set51 switch (archType) {52 case ArchtypeMap:53 return new Map(value)54 case ArchtypeSet:55 // Set will be cloned as array temporarily, so that we can replace individual items56 return Array.from(value)57 }58 return shallowCopy(value)

Basic implementation for `current`Michel Weststratebefore adoption7322e82

line 59why, one step back
59}