Skip to content
Myr

feat: `onCopy` 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 aa24f65 · Browse files · 2 files+20 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

2 files+20 added−3 removed

  • tests:+18 added−0 removed
  • other:+2 added−3 removed

2 files+20 added−3 removed

modified:__tests__/hooks.js+18 added
281 unmodified lines
282 )283 expectCalls(onDelete)284 })285 })286287 describe("when draft is a Set -", () => {288 test("delete", () => {289 produce({a: new Set([1])}, s => {290 s.a.delete(1)291 })292 expect(onDelete).not.toBeCalled()293 })294 })295 }296 })297298 describe("onCopy()", () => {
21 unmodified lines
320 s.get("a").delete("b")321 })322 expect(calls).toShallowEqual([base.get("a"), base])323 })324325 it("is called in the right order for Sets", () => {326 const item1 = {a: 0}327 const base = new Set([item1])328 produce(base, s => {329 s.forEach(item => item.a++)330 })331 expect(calls).toShallowEqual([item1, base])332 })333 }334 })335336 function useSharedTests(getHook) {
44 unmodified lines
modified:src/immer.js+2 added-3 removed
188 unmodified lines
189 if (!state.finalized) {190 state.finalized = true191 this.finalizeTree(state.draft, path, scope)192193 // TODO: It won't fire for Sets because they don't use `assigned`. Is it an issue?194 if (this.onDelete) {193 // We cannot really delete anything inside of a Set. We can only replace the whole Set.194 if (this.onDelete && !isSet(state.base)) {195 // The `assigned` object is unreliable with ES5 drafts.196 if (this.useProxies) {197 const {assigned} = state198 each(assigned, (prop, assignedValue) => {
71 unmodified lines
270271 setProperty(parent, prop, value)272273 // Unchanged drafts are never passed to the `onAssign` hook.274 // TODO: Add tests and support for Sets274 if (isDraftProp && value === get(state.base, prop)) return275 }276 // Unchanged draft properties are ignored.277 else if (isDraftProp && is(value, get(state.base, prop))) {
36 unmodified lines