Skip to content
Myr

feat: `onAssign` for `Sets`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by keenondrums · committed by Alec Larson · parent 8de78f2 · Browse files · 3 files+85 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+85 added−3 removed

  • tests:+79 added−0 removed
  • other:+6 added−3 removed

3 files+85 added−3 removed

modified:__tests__/__snapshots__/hooks.js.snap+41 added
193 unmodified lines
194 ],195]196`;197198exports[`hooks (proxy) - onAssign() when draft is a Set assign 1`] = `199Array [200 Array [201 "a",202 Set {203 1,204 2,205 3,206 4,207 },208 ],209]210`;211212exports[`hooks (proxy) - onAssign() when draft is a Set delete 1`] = `213Array [214 Array [215 "a",216 Set {217 2,218 3,219 },220 ],221]222`;223224exports[`hooks (proxy) - onAssign() when draft is a Set nested assignments 1`] = `225Array [226 Array [227 "a",228 Set {229 "a",230 Set {231 1,232 2,233 },234 },235 ],236]237`;238239exports[`hooks (proxy) - onAssign() when draft is an array assign 1`] = `240Array [241 Array [242 0,
161 unmodified lines
modified:__tests__/hooks.js+38 added
1"use strict"2import {Immer, setUseProxies} from "../src/index"3import matchers from "expect/build/matchers"4import {isSet} from "../src/common"56describe("hooks (proxy) -", () => createHookTests(true))7describe("hooks (es5) -", () => createHookTests(false))8
157 unmodified lines
166 )167 expectCalls(onAssign)168 })169 })170171 describe("when draft is a Set", () => {172 test("assign", () => {173 produce({a: new Set([1, 2, 3])}, s => {174 s.a.add(4)175 })176 expectCalls(onAssign)177 })178 test("assign (no change)", () => {179 produce({a: new Set([1, 2, 3])}, s => {180 s.a.add(3)181 })182 expect(onAssign).not.toBeCalled()183 })184 // Any mutation of a Set results in a new assignment. Including deletes.185 test("delete", () => {186 produce({a: new Set([1, 2, 3])}, s => {187 s.a.delete(1)188 })189 expectCalls(onAssign)190 })191 test("nested assignments", () => {192 const val1 = {prop: "val1"}193 produce({a: new Set(["a", new Set([val1, 1])])}, s => {194 let nested195 s.a.forEach(value => {196 if (isSet(value)) {197 nested = value198 }199 })200 nested.add(2)201 nested.delete(val1)202 nested.add(1) // no-op203 })204 expectCalls(onAssign)205 })206 })207 }208 })209210 describe("onDelete()", () => {
152 unmodified lines
modified:src/immer.js+6 added-3 removed
12 unmodified lines
13 isMap,14 shallowCopy,15 DRAFT_STATE,16 NOTHING,17 isSet17 isSet,18 original19} from "./common"20import {ImmerScope} from "./scope"2122function verifyMinified() {}
225 unmodified lines
248 }249250 // In the `finalizeTree` method, only the `root` object may be a draft.251 const isDraftProp = !!state && parent === root252 const isSetMember = isSet(parent)253254 if (isDraft(value)) {255 const path =256 isDraftProp &&257 needPatches &&256 !isSet(parent) &&258 !isSetMember &&259 !has(state.assigned, prop)260 ? rootPath.concat(prop)261 : null262
19 unmodified lines
282 else if (isDraftable(value) && !Object.isFrozen(value)) {283 each(value, finalizeProperty)284 }285286 // We cannot really assign anything inside of a Set. We can only replace the whole Set.284 if (isDraftProp && this.onAssign) {287 if (isDraftProp && this.onAssign && !isSetMember) {288 this.onAssign(state, prop, value)289 }290 }291
23 unmodified lines