current.ts
This file
Path
src/core/current.ts
60 lines · 1.5 KiB · TypeScript
Last changed by
Some dotsMichel Weststratebefore adoption4e438d0
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import { die, isDraft, shallowCopy, each, DRAFT_STATE, get, set, ImmerState, isDraftable, ArchtypeMap, ArchtypeSet, getArchtype} from "../internal"import {getPlugin} from "../utils/plugins"
export function current<T>(value: T): Texport function current(value: any): any { if (!isDraft(value)) die(22, value) return currentImpl(value)}
function currentImpl(value: any): any { if (!isDraftable(value)) return value const state: ImmerState | undefined = value[DRAFT_STATE] let copy: any const archType = getArchtype(value) if (state) { if ( !state.modified_ && (state.type_ < 4 || !getPlugin("ES5").hasChanges_(state as any)) ) return state.base_ // Optimization: avoid generating new drafts during copying state.finalized_ = true copy = copyHelper(value, archType) state.finalized_ = false } else { copy = copyHelper(value, archType) }
each(copy, (key, childValue) => { if (state && get(state.base_, key) === childValue) return // no need to copy or search in something that didn't change set(copy, key, currentImpl(childValue)) }) // In the future, we might consider freezing here, based on the current settings return archType === ArchtypeSet ? new Set(copy) : copy}
function copyHelper(value: any, archType: number): any { // creates a shallow copy, even if it is a map or set switch (archType) { case ArchtypeMap: return new Map(value) case ArchtypeSet: // Set will be cloned as array temporarily, so that we can replace individual items return Array.from(value) } return shallowCopy(value)}