current.ts
This file
Path
src/core/current.ts
38 lines · 881 bytes · TypeScript
Last changed by
Basic implementation for `current`Michel Weststratebefore adoption7322e82
History of this fileTake it
Basic implementation for `current`Michel Weststratebefore adoption7322e82
lines 1–38why, one step back1import {2 die,3 isDraft,4 shallowCopy,5 each,6 DRAFT_STATE,7 get,8 set,9 ImmerState,10 isDraftable11} from "../internal"1213export function current<T>(value: T): T14export function current(value: any): any {15 if (!isDraft(value)) die(22, value)16 return currentImpl(value)17}1819function currentImpl(value: any): any {20 if (!isDraftable(value)) return value21 const state: ImmerState | undefined = value[DRAFT_STATE]22 let copy: any23 if (state) {24 if (!state.modified_) return state.base_25 // Optimization: avoid generating new drafts during copying26 state.finalized_ = true27 copy = shallowCopy(value)28 state.finalized_ = false29 } else {30 copy = shallowCopy(value)31 }3233 each(copy, (key, childValue) => {34 if (state && get(state.base_, key) === childValue) return // no need to copy a current of something that didn't change35 set(copy, key, currentImpl(childValue))36 })37 return copy38}