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 fileTake it
Basic implementation for `current`Michel Weststratebefore adoption7322e82
lines 1–9why, one step back1import {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 back10 isDraftable,11 ArchtypeMap,12 ArchtypeSet,13 getArchtypeBasic implementation for `current`Michel Weststratebefore adoption7322e82
line 14why, one step back14} from "../internal"Made ES5 version workMichel Weststratebefore adoption870f44e
line 15why, one step back15import {getPlugin} from "../utils/plugins"Basic implementation for `current`Michel Weststratebefore adoption7322e82
lines 16–26why, one step back1617export 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: anyFurther current implMichel Weststratebefore adoption5036e7f
line 27why, one step back27 const archType = getArchtype(value)Basic implementation for `current`Michel Weststratebefore adoption7322e82
line 28why, one step back28 if (state) {Made ES5 version workMichel Weststratebefore adoption870f44e
lines 29–33why, one step back29 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 back34 // Optimization: avoid generating new drafts during copying35 state.finalized_ = trueFurther current implMichel Weststratebefore adoption5036e7f
line 36why, one step back36 copy = copyHelper(value, archType)Basic implementation for `current`Michel Weststratebefore adoption7322e82
lines 37–38why, one step back37 state.finalized_ = false38 } else {Further current implMichel Weststratebefore adoption5036e7f
line 39why, one step back39 copy = copyHelper(value, archType)Basic implementation for `current`Michel Weststratebefore adoption7322e82
lines 40–42why, one step back40 }4142 each(copy, (key, childValue) => {Further current implMichel Weststratebefore adoption5036e7f
line 43why, one step back43 if (state && get(state.base_, key) === childValue) return // no need to copy or search in something that didn't changeBasic implementation for `current`Michel Weststratebefore adoption7322e82
lines 44–45why, one step back44 set(copy, key, currentImpl(childValue))45 })Further current implMichel Weststratebefore adoption5036e7f
lines 46–58why, one step back46 return archType === ArchtypeSet ? new Set(copy) : copy47}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 back59}