Further current impl
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 7322e82 · Browse files · 2 files·+41 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+41 added−20 removed
- tests:+20 added−15 removed
- other:+21 added−5 removed
Provenance
- Commit
5036e7f- Tree
d0ac97b- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
7322e82
2 files changed+41 added−20 removed
modified:__tests__/current.js+20 added-15 removed
10 unmodified lines11 12 enableAllPlugins()13 14 runTests("proxy", true)15-// TODO: enable16-// runTests("es5", false)15+runTests("es5", false)16 17 function runTests(name, useProxies) {18 describe("current - " + name, () => {19 beforeAll(() => {6 unmodified lines26 current({})27 }).toThrowError("[Immer] 'current' expects a draft, got: [object Object]")28 })29 31- it("can handle simple objects", () => {32- const base = {x: 1}30+ it("can handle simple arrays", () => {31+ const base = [{x: 1}]32 let c33 const next = produce(base, draft => {35- draft.x++36- debugger34+ expect(current(draft)).toEqual(base)35+ draft[0].x++36 c = current(draft)37+ expect(c).toEqual([{x: 2}])38+ expect(Array.isArray(c))38- draft.x++39+ draft[0].x++40 })40- expect(next).toEqual({x: 3})41- expect(c).toEqual({x: 2})41+ expect(next).toEqual([{x: 3}])42+ expect(c).toEqual([{x: 2}])43 expect(isDraft(c)).toBe(false)44 })45 46 it("won't freeze", () => {90 unmodified lines137 expect(current(draft)).toBe(base)138 draft.delete("a")139 let c = current(draft)140 expect(current(draft)).not.toBe(base)141+ expect(current(draft)).not.toBe(draft)142 expect(c).toEqual(new Map())143 const obj = {}144 draft.set("b", obj)145 expect(c).toEqual(new Map())146 expect(current(draft)).toEqual(new Map([["b", obj]]))145- expect(current(draft).get("b")).toBe(obj)147+ expect(c).toBeInstanceOf(Map)148 })149 })150 151 it("handles map - 2", () => {11 unmodified lines163 it("handles set", () => {164 const base = new Set([1])165 produce(base, draft => {166 expect(current(draft)).toBe(base)165- base.add(2)167+ draft.add(2)168 const c = current(draft)169 expect(c).toEqual(new Set([1, 2]))170 expect(c).not.toBe(draft)171+ expect(c).not.toBe(base)169- base.add(3)172+ draft.add(3)173 expect(c).toEqual(new Set([1, 2]))174+ expect(c).toBeInstanceOf(Set)175 })176 })177 178 it("handles simple class", () => {5 unmodified lines184 this.current++185 }186 }187 184- const c = new Counter()185- produce(c, draft => {186- expect(current(draft)).toBe(c)188+ const counter1 = new Counter()189+ produce(counter1, draft => {190+ expect(current(draft)).toBe(counter1)191 draft.inc()192 const c = current(draft)193 expect(c).not.toBe(draft)194 expect(c.current).toBe(1)3 unmodified lines198 draft.inc()199 draft.inc()200 expect(c.current).toBe(2)201 expect(draft.current).toBe(3)202+ expect(c).toBeInstanceOf(Counter)203 })204 })205 })206 }modified:src/core/current.ts+21 added-5 removed
5 unmodified lines67891010111213141516174 unmodified lines222324252627282930273132333034353637343839403741424344454647484950515253545 unmodified lines DRAFT_STATE, get, set, ImmerState, isDraftable isDraftable, ArchtypeMap, ArchtypeSet, getArchtype} from "../internal"
export function current<T>(value: T): Texport function current(value: any): any {4 unmodified linesfunction 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_) return state.base_ // Optimization: avoid generating new drafts during copying state.finalized_ = true copy = shallowCopy(value) copy = copyHelper(value, archType) state.finalized_ = false } else { copy = shallowCopy(value) copy = copyHelper(value, archType) }
each(copy, (key, childValue) => { if (state && get(state.base_, key) === childValue) return // no need to copy a current of something that didn't change 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)) }) return copy 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)}