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
Provenance
- Commit
aa24f65- Tree
f99306f- Author
keenondrums <andrey.goncharov+it@protonmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
8de78f2
3 files changed+85 added−3 removed
modified:__tests__/__snapshots__/hooks.js.snap+41 added
193 unmodified lines194 ],195 ]196 `;197 198+exports[`hooks (proxy) - onAssign() when draft is a Set assign 1`] = `199+Array [200+ Array [201+ "a",202+ Set {203+ 1,204+ 2,205+ 3,206+ 4,207+ },208+ ],209+]210+`;211+212+exports[`hooks (proxy) - onAssign() when draft is a Set delete 1`] = `213+Array [214+ Array [215+ "a",216+ Set {217+ 2,218+ 3,219+ },220+ ],221+]222+`;223+224+exports[`hooks (proxy) - onAssign() when draft is a Set nested assignments 1`] = `225+Array [226+ Array [227+ "a",228+ Set {229+ "a",230+ Set {231+ 1,232+ 2,233+ },234+ },235+ ],236+]237+`;238+239 exports[`hooks (proxy) - onAssign() when draft is an array assign 1`] = `240 Array [241 Array [242 0,161 unmodified linesmodified:__tests__/hooks.js+38 added
1 "use strict"2 import {Immer, setUseProxies} from "../src/index"3 import matchers from "expect/build/matchers"4+import {isSet} from "../src/common"5 6 describe("hooks (proxy) -", () => createHookTests(true))7 describe("hooks (es5) -", () => createHookTests(false))8 157 unmodified lines166 )167 expectCalls(onAssign)168 })169 })170+171+ 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 })209 210 describe("onDelete()", () => {152 unmodified linesmodified:src/immer.js+6 added-3 removed
12 unmodified lines13 isMap,14 shallowCopy,15 DRAFT_STATE,16 NOTHING,17- isSet17+ isSet,18+ original19 } from "./common"20 import {ImmerScope} from "./scope"21 22 function verifyMinified() {}225 unmodified lines248 }249 250 // In the `finalizeTree` method, only the `root` object may be a draft.251 const isDraftProp = !!state && parent === root252+ const isSetMember = isSet(parent)253 254 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 lines282 else if (isDraftable(value) && !Object.isFrozen(value)) {283 each(value, finalizeProperty)284 }285 286+ // 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