feat: `onDelete` for `Maps`
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 eaea1b0 · Browse files · 4 files·+65 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+65 added−7 removed
- tests:+55 added−0 removed
- other:+10 added−7 removed
Provenance
- Commit
1ca49ef- Tree
4b2df54- Author
keenondrums <andrey.goncharov+it@protonmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
eaea1b0
4 files changed+65 added−7 removed
modified:__tests__/__snapshots__/hooks.js.snap+21 added
301 unmodified lines302 ],303 ]304 `;305 306+exports[`hooks (proxy) - onDelete() when draft is a Map - delete 1`] = `307+Array [308+ Array [309+ "a",310+ ],311+ Array [312+ Object {313+ "prop": "val1",314+ },315+ ],316+]317+`;318+319+exports[`hooks (proxy) - onDelete() when draft is a Map - nested deletions 1`] = `320+Array [321+ Array [322+ "b",323+ ],324+]325+`;326+327 exports[`hooks (proxy) - onDelete() when draft is an array - length = 0 1`] = `Array []`;328 329 exports[`hooks (proxy) - onDelete() when draft is an array - pop 1`] = `330 Array [32 unmodified linesmodified:__tests__/hooks.js+34 added
210 unmodified lines211 })212 expectCalls(onDelete)213 })214 })215+216+ if (useProxies) {217+ describe("when draft is a Map -", () => {218+ test("delete", () => {219+ const key1 = {prop: "val1"}220+ const key2 = {prop: "val2"}221+ produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {222+ s.delete("a")223+ s.delete(key1)224+ })225+ expectCalls(onDelete)226+ })227+ test("delete (no change)", () => {228+ produce(new Map(), s => {229+ s.delete("a")230+ })231+ expect(onDelete).not.toBeCalled()232+ })233+ test("nested deletions", () => {234+ const key1 = {prop: "val1"}235+ produce(236+ new Map([237+ ["a", new Map([[key1, new Map([["b", 1]])]])]238+ ]),239+ s => {240+ s.get("a")241+ .get(key1)242+ .delete("b")243+ }244+ )245+ expectCalls(onDelete)246+ })247+ })248+ }249 })250 251 describe("onCopy()", () => {252 useSharedTests(() => onCopy)58 unmodified linesmodified:src/immer.js+4 added-4 removed
192 unmodified lines193 if (this.onDelete) {194 // The `assigned` object is unreliable with ES5 drafts.195 if (this.useProxies) {196 const {assigned} = state197- for (const prop in assigned) {198- if (!assigned[prop]) this.onDelete(state, prop)199- }197+ each(assigned, (prop, assignedValue) => {198+ if (!assignedValue) this.onDelete(state, prop)199+ })200 } else {201 // TODO: Figure it out for Maps and Sets if we need to support ES5202 const {base, copy} = state203 each(base, prop => {64 unmodified lines268 269 setProperty(parent, prop, value)270 271 // Unchanged drafts are never passed to the `onAssign` hook.272- // TODO: Add tests and support for Maps and Sets272+ // TODO: Add tests and support for Sets273 if (isDraftProp && value === get(state.base, prop)) return274 }275 // Unchanged draft properties are ignored.276 else if (isDraftProp && is(value, get(state.base, prop))) {35 unmodified linesmodified:src/proxy.js+6 added-3 removed
213 unmodified lines214 }215 return state.draft216 },217 delete: state => key => {218+ if (source(state).has(key)) {218- markChanged(state)219- state.assigned.set(key, false)220- return state.copy.delete(key)219+ markChanged(state)220+ state.assigned.set(key, false)221+ return state.copy.delete(key)222+ }223+ return false224 },225 clear: state => () => {226 markChanged(state)227 state.assigned = new Map()207 unmodified lines