Skip to content
Myr

killed hooks, as they leaked mangled properties. Also -145 bytes

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 d77a5c1 · Browse files · 7 files+23 added−969 removed
This commit

State

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

Diff stats

7 files+23 added−969 removed

  • tests:+0 added−900 removed
  • other:+23 added−69 removed

7 files+23 added−969 removed

deleted:__tests__/__snapshots__/hooks.js.snap-509 removed
1// Jest Snapshot v1, https://goo.gl/fbAQLP23exports[`hooks (es5) - onAssign() when draft is a Map assign 1`] = `4Array [5 Array [6 "a",7 10,8 ],9 Array [10 Object {11 "prop": "val1",12 },13 11,14 ],15]16`;1718exports[`hooks (es5) - onAssign() when draft is a Map nested assignments 1`] = `19Array [20 Array [21 "b",22 2,23 ],24 Array [25 Object {26 "prop": "val1",27 },28 Map {29 "b" => 2,30 "d" => 1,31 },32 ],33 Array [34 "a",35 Map {36 Object {37 "prop": "val1",38 } => Map {39 "b" => 2,40 "d" => 1,41 },42 },43 ],44]45`;4647exports[`hooks (es5) - onAssign() when draft is a Set assign 1`] = `48Array [49 Array [50 "a",51 Set {52 1,53 2,54 3,55 4,56 },57 ],58]59`;6061exports[`hooks (es5) - onAssign() when draft is a Set delete 1`] = `62Array [63 Array [64 "a",65 Set {66 2,67 3,68 },69 ],70]71`;7273exports[`hooks (es5) - onAssign() when draft is a Set nested assignments 1`] = `74Array [75 Array [76 "a",77 Set {78 "a",79 Set {80 1,81 2,82 },83 },84 ],85]86`;8788exports[`hooks (es5) - onAssign() when draft is an array assign 1`] = `89Array [90 Array [91 0,92 0,93 ],94]95`;9697exports[`hooks (es5) - onAssign() when draft is an array push 1`] = `98Array [99 Array [100 0,101 4,102 ],103]104`;105106exports[`hooks (es5) - onAssign() when draft is an array splice (length += 0) 1`] = `107Array [108 Array [109 1,110 0,111 ],112]113`;114115exports[`hooks (es5) - onAssign() when draft is an array splice (length += 1) 1`] = `116Array [117 Array [118 1,119 0,120 ],121 Array [122 2,123 0,124 ],125 Array [126 3,127 3,128 ],129]130`;131132exports[`hooks (es5) - onAssign() when draft is an array splice (length -= 1) 1`] = `133Array [134 Array [135 0,136 6,137 ],138 Array [139 1,140 3,141 ],142]143`;144145exports[`hooks (es5) - onAssign() when draft is an array unshift 1`] = `146Array [147 Array [148 0,149 0,150 ],151 Array [152 1,153 1,154 ],155]156`;157158exports[`hooks (es5) - onAssign() when draft is an object assign 1`] = `159Array [160 Array [161 "a",162 1,163 ],164 Array [165 "c",166 1,167 ],168]169`;170171exports[`hooks (es5) - onAssign() when draft is an object nested assignments 1`] = `172Array [173 Array [174 "c",175 2,176 ],177 Array [178 "b",179 Object {180 "c": 2,181 "e": 1,182 },183 ],184 Array [185 "a",186 Object {187 "b": Object {188 "c": 2,189 "e": 1,190 },191 },192 ],193]194`;195196exports[`hooks (es5) - onDelete() when draft is a Map - delete 1`] = `197Array [198 Array [199 "a",200 ],201 Array [202 Object {203 "prop": "val1",204 },205 ],206]207`;208209exports[`hooks (es5) - onDelete() when draft is a Map - nested deletions 1`] = `210Array [211 Array [212 "b",213 ],214]215`;216217exports[`hooks (es5) - onDelete() when draft is an array - length = 0 1`] = `218Array [219 Array [220 0,221 ],222]223`;224225exports[`hooks (es5) - onDelete() when draft is an array - pop 1`] = `226Array [227 Array [228 0,229 ],230]231`;232233exports[`hooks (es5) - onDelete() when draft is an array - splice (length -= 1) 1`] = `234Array [235 Array [236 2,237 ],238]239`;240241exports[`hooks (es5) - onDelete() when draft is an object - delete 1`] = `242Array [243 Array [244 "a",245 ],246 Array [247 "c",248 ],249]250`;251252exports[`hooks (es5) - onDelete() when draft is an object - nested deletions 1`] = `253Array [254 Array [255 "c",256 ],257]258`;259260exports[`hooks (proxy) - onAssign() when draft is a Map assign 1`] = `261Array [262 Array [263 "a",264 10,265 ],266 Array [267 Object {268 "prop": "val1",269 },270 11,271 ],272]273`;274275exports[`hooks (proxy) - onAssign() when draft is a Map nested assignments 1`] = `276Array [277 Array [278 "b",279 2,280 ],281 Array [282 Object {283 "prop": "val1",284 },285 Map {286 "b" => 2,287 "d" => 1,288 },289 ],290 Array [291 "a",292 Map {293 Object {294 "prop": "val1",295 } => Map {296 "b" => 2,297 "d" => 1,298 },299 },300 ],301]302`;303304exports[`hooks (proxy) - onAssign() when draft is a Set assign 1`] = `305Array [306 Array [307 "a",308 Set {309 1,310 2,311 3,312 4,313 },314 ],315]316`;317318exports[`hooks (proxy) - onAssign() when draft is a Set delete 1`] = `319Array [320 Array [321 "a",322 Set {323 2,324 3,325 },326 ],327]328`;329330exports[`hooks (proxy) - onAssign() when draft is a Set nested assignments 1`] = `331Array [332 Array [333 "a",334 Set {335 "a",336 Set {337 1,338 2,339 },340 },341 ],342]343`;344345exports[`hooks (proxy) - onAssign() when draft is an array assign 1`] = `346Array [347 Array [348 0,349 0,350 ],351]352`;353354exports[`hooks (proxy) - onAssign() when draft is an array push 1`] = `355Array [356 Array [357 0,358 4,359 ],360]361`;362363exports[`hooks (proxy) - onAssign() when draft is an array splice (length += 0) 1`] = `364Array [365 Array [366 1,367 0,368 ],369]370`;371372exports[`hooks (proxy) - onAssign() when draft is an array splice (length += 1) 1`] = `373Array [374 Array [375 1,376 0,377 ],378 Array [379 2,380 0,381 ],382 Array [383 3,384 3,385 ],386]387`;388389exports[`hooks (proxy) - onAssign() when draft is an array splice (length -= 1) 1`] = `390Array [391 Array [392 0,393 6,394 ],395 Array [396 1,397 3,398 ],399]400`;401402exports[`hooks (proxy) - onAssign() when draft is an array unshift 1`] = `403Array [404 Array [405 0,406 0,407 ],408 Array [409 1,410 1,411 ],412]413`;414415exports[`hooks (proxy) - onAssign() when draft is an object assign 1`] = `416Array [417 Array [418 "a",419 1,420 ],421 Array [422 "c",423 1,424 ],425]426`;427428exports[`hooks (proxy) - onAssign() when draft is an object nested assignments 1`] = `429Array [430 Array [431 "c",432 2,433 ],434 Array [435 "b",436 Object {437 "c": 2,438 "e": 1,439 },440 ],441 Array [442 "a",443 Object {444 "b": Object {445 "c": 2,446 "e": 1,447 },448 },449 ],450]451`;452453exports[`hooks (proxy) - onDelete() when draft is a Map - delete 1`] = `454Array [455 Array [456 "a",457 ],458 Array [459 Object {460 "prop": "val1",461 },462 ],463]464`;465466exports[`hooks (proxy) - onDelete() when draft is a Map - nested deletions 1`] = `467Array [468 Array [469 "b",470 ],471]472`;473474exports[`hooks (proxy) - onDelete() when draft is an array - length = 0 1`] = `Array []`;475476exports[`hooks (proxy) - onDelete() when draft is an array - pop 1`] = `477Array [478 Array [479 "0",480 ],481]482`;483484exports[`hooks (proxy) - onDelete() when draft is an array - splice (length -= 1) 1`] = `485Array [486 Array [487 "2",488 ],489]490`;491492exports[`hooks (proxy) - onDelete() when draft is an object - delete 1`] = `493Array [494 Array [495 "a",496 ],497 Array [498 "c",499 ],500]501`;502503exports[`hooks (proxy) - onDelete() when draft is an object - nested deletions 1`] = `504Array [505 Array [506 "c",507 ],508]509`;
deleted:__tests__/hooks.js-391 removed
1"use strict"2import {Immer, setUseProxies, enableAllPlugins} from "../src/immer"3import matchers from "expect/build/matchers"4import {isSet} from "../src/common"56enableAllPlugins()78describe("hooks (proxy) -", () => createHookTests(true))9describe("hooks (es5) -", () => createHookTests(false))1011function createHookTests(useProxies) {12 let produce, onAssign, onDelete, onCopy1314 beforeEach(() => {15 ;({produce, onAssign, onDelete, onCopy} = new Immer({16 autoFreeze: true,17 useProxies,18 onAssign: defuseProxies(jest.fn().mockName("onAssign")),19 onDelete: defuseProxies(jest.fn().mockName("onDelete")),20 onCopy: defuseProxies(jest.fn().mockName("onCopy"))21 }))22 })2324 describe("onAssign()", () => {25 useSharedTests(() => onAssign)26 describe("when draft is an object", () => {27 test("assign", () => {28 produce({a: 0, b: 0, c: 0}, s => {29 s.a++30 s.c++31 })32 expectCalls(onAssign)33 })34 test("assign (no change)", () => {35 produce({a: 0}, s => {36 s.a = 037 })38 expect(onAssign).not.toBeCalled()39 })40 test("delete", () => {41 produce({a: 1}, s => {42 delete s.a43 })44 expect(onAssign).not.toBeCalled()45 })46 test("nested assignments", () => {47 produce({a: {b: {c: 1, d: 1, e: 1}}}, s => {48 const {b} = s.a49 b.c = 250 delete b.d51 b.e = 1 // no-op52 })53 expectCalls(onAssign)54 })55 })56 describe("when draft is an array", () => {57 test("assign", () => {58 produce([1], s => {59 s[0] = 060 })61 expectCalls(onAssign)62 })63 test("push", () => {64 produce([], s => {65 s.push(4)66 })67 expectCalls(onAssign)68 })69 test("pop", () => {70 produce([1], s => {71 s.pop()72 })73 expect(onAssign).not.toBeCalled()74 })75 test("unshift", () => {76 produce([1], s => {77 s.unshift(0)78 })79 expectCalls(onAssign)80 })81 test("length = 0", () => {82 produce([1], s => {83 s.length = 084 })85 expect(onAssign).not.toBeCalled()86 })87 test("splice (length += 1)", () => {88 produce([1, 2, 3], s => {89 s.splice(1, 1, 0, 0)90 })91 expectCalls(onAssign)92 })93 test("splice (length += 0)", () => {94 produce([1, 2, 3], s => {95 s.splice(1, 1, 0)96 })97 expectCalls(onAssign)98 })99 test("splice (length -= 1)", () => {100 produce([1, 2, 3], s => {101 s.splice(0, 2, 6)102 })103 expectCalls(onAssign)104 })105 })106 describe("when a draft is moved into a new object", () => {107 it("is called in the right order", () => {108 const calls = []109 onAssign.mockImplementation((_, prop) => {110 calls.push(prop)111 })112 produce({a: {b: 1, c: {}}}, s => {113 s.a.b = 0114 s.a.c.d = 1115 s.x = {y: {z: s.a}}116 delete s.a117 })118 // Sibling properties use enumeration order, which means new119 // properties come last among their siblings. The deepest120 // properties always come first in their ancestor chain.121 expect(calls).toEqual(["b", "d", "c", "x"])122 })123 })124125 describe("when draft is a Map", () => {126 test("assign", () => {127 const key1 = {prop: "val1"}128 const key2 = {prop: "val2"}129 produce(130 new Map([131 ["a", 0],132 [key1, 1],133 [key2, 2]134 ]),135 s => {136 s.set("a", 10)137 s.set(key1, 11)138 }139 )140 expectCalls(onAssign)141 })142 test("assign (no change)", () => {143 produce(new Map([["a", 0]]), s => {144 s.set("a", 0)145 })146 expect(onAssign).not.toBeCalled()147 })148 test("delete", () => {149 produce(new Map([["a", 0]]), s => {150 s.delete("a")151 })152 expect(onAssign).not.toBeCalled()153 })154 test("nested assignments", () => {155 const key1 = {prop: "val1"}156 produce(157 new Map([158 [159 "a",160 new Map([161 [162 key1,163 new Map([164 ["b", 1],165 ["c", 1],166 ["d", 1]167 ])168 ]169 ])170 ]171 ]),172 s => {173 const nested = s.get("a").get(key1)174 nested.set("b", 2)175 nested.delete("c")176 nested.set("d", 1) // no-op177 }178 )179 expectCalls(onAssign)180 })181 })182183 describe("when draft is a Set", () => {184 test("assign", () => {185 produce({a: new Set([1, 2, 3])}, s => {186 s.a.add(4)187 })188 expectCalls(onAssign)189 })190 test("assign (no change)", () => {191 produce({a: new Set([1, 2, 3])}, s => {192 s.a.add(3)193 })194 expect(onAssign).not.toBeCalled()195 })196 // Any mutation of a Set results in a new assignment. Including deletes.197 test("delete", () => {198 produce({a: new Set([1, 2, 3])}, s => {199 s.a.delete(1)200 })201 expectCalls(onAssign)202 })203 test("nested assignments", () => {204 const val1 = {prop: "val1"}205 produce({a: new Set(["a", new Set([val1, 1])])}, s => {206 let nested207 s.a.forEach(value => {208 if (value instanceof Set) {209 nested = value210 }211 })212 nested.add(2)213 nested.delete(val1)214 nested.add(1) // no-op215 })216 expectCalls(onAssign)217 })218 })219 })220221 describe("onDelete()", () => {222 useSharedTests(() => onDelete)223 describe("when draft is an object -", () => {224 test("delete", () => {225 produce({a: 1, b: 1, c: 1}, s => {226 delete s.a227 delete s.c228 })229 expectCalls(onDelete)230 })231 test("delete (no change)", () => {232 produce({}, s => {233 delete s.a234 })235 expect(onDelete).not.toBeCalled()236 })237 test("nested deletions", () => {238 produce({a: {b: {c: 1}}}, s => {239 delete s.a.b.c240 })241 expectCalls(onDelete)242 })243 })244 describe("when draft is an array -", () => {245 test("pop", () => {246 produce([1], s => {247 s.pop()248 })249 expectCalls(onDelete)250 })251 test("length = 0", () => {252 produce([1], s => {253 s.length = 0254 })255 expectCalls(onDelete)256 })257 test("splice (length -= 1)", () => {258 produce([1, 2, 3], s => {259 s.splice(0, 2, 6)260 })261 expectCalls(onDelete)262 })263 })264265 describe("when draft is a Map -", () => {266 test("delete", () => {267 const key1 = {prop: "val1"}268 const key2 = {prop: "val2"}269 produce(270 new Map([271 ["a", 0],272 [key1, 1],273 [key2, 2]274 ]),275 s => {276 s.delete("a")277 s.delete(key1)278 }279 )280 expectCalls(onDelete)281 })282 test("delete (no change)", () => {283 produce(new Map(), s => {284 s.delete("a")285 })286 expect(onDelete).not.toBeCalled()287 })288 test("nested deletions", () => {289 const key1 = {prop: "val1"}290 produce(new Map([["a", new Map([[key1, new Map([["b", 1]])]])]]), s => {291 s.get("a")292 .get(key1)293 .delete("b")294 })295 expectCalls(onDelete)296 })297 })298299 describe("when draft is a Set -", () => {300 test("delete", () => {301 produce({a: new Set([1])}, s => {302 s.a.delete(1)303 })304 expect(onDelete).not.toBeCalled()305 })306 })307 })308309 describe("onCopy()", () => {310 if (global.USES_BUILD) return311312 let calls313 beforeEach(() => {314 calls = []315 onCopy.mockImplementation(s => {316 calls.push(s.base_)317 })318 })319320 useSharedTests(() => onCopy)321 it("is called in the right order for objects", () => {322 const base = {a: {b: {c: 1}}}323 produce(base, s => {324 delete s.a.b.c325 })326 expect(calls).toShallowEqual([base.a.b, base.a, base])327 })328329 it("is called in the right order for Maps", () => {330 const base = new Map([["a", new Map([["b", 0]])]])331 produce(base, s => {332 s.get("a").delete("b")333 })334 expect(calls).toShallowEqual([base.get("a"), base])335 })336337 it("is called in the right order for Sets", () => {338 const item1 = {a: 0}339 const base = new Set([item1])340 produce(base, s => {341 s.forEach(item => item.a++)342 })343 expect(calls).toShallowEqual([item1, base])344 })345 })346347 function useSharedTests(getHook) {348 it("is called before the parent is frozen", () => {349 const hook = getHook()350 hook.mockImplementation(s => {351 // Parent object must not be frozen.352 if (!global.USES_BUILD) expect(Object.isFrozen(s.base_)).toBeFalsy()353 })354 produce({a: {b: {c: 0}}}, s => {355 if (hook == onDelete) delete s.a.b.c356 else s.a.b.c = 1357 })358 expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3)359 })360 }361}362363// Produce a snapshot of the hook arguments (minus any draft state).364function expectCalls(hook) {365 expect(366 hook.mock.calls.map(call => {367 return call.slice(1)368 })369 ).toMatchSnapshot()370}371372// For defusing draft proxies.373function defuseProxies(fn) {374 return Object.assign((...args) => {375 if (!global.USES_BUILD) expect(args[0].finalized_).toBeTruthy()376 args[0].draft = args[0].drafts = null377 fn(...args)378 }, fn)379}380381expect.extend({382 toShallowEqual(received, expected) {383 const match = matchers.toBe(received, expected)384 return match.pass || !received || typeof received !== "object"385 ? match386 : !Array.isArray(expected) ||387 (Array.isArray(received) && received.length === expected.length)388 ? matchers.toEqual(received, expected)389 : match390 }391})
modified:docs/api.md+1 added-1 removed
modified:src/plugins/patches.ts+2 added-2 removed
55 unmodified lines
56 basePath: PatchPath,57 patches: Patch[],58 inversePatches: Patch[]59 ) {60 const {base_, assigned_} = state61 const copy_ = state.copy_!60 let {base_, assigned_} = state61 let copy_ = state.copy_!6263 // Reduce complexity by ensuring `base` is never longer.64 if (copy_.length < base_.length) {65 // @ts-ignore
198 unmodified lines
modified:src/finalize.ts+2 added-25 removed
81 unmodified lines
82 if (!state.finalized_) {83 state.finalized_ = true84 finalizeTree(immer, state.draft_, scope, path)8586 // We cannot really delete anything inside of a Set. We can only replace the whole Set.87 if (immer.onDelete && state.type_ !== ProxyType.Set) {88 // The `assigned` object is unreliable with ES5 drafts.89 if (immer.useProxies) {90 const {assigned_} = state91 each(assigned_ as any, (prop, exists) => {92 if (!exists) immer.onDelete!(state, prop as any)93 })94 } else {95 const {base_, copy_} = state96 each(base_, prop => {97 if (!has(copy_, prop)) immer.onDelete!(state, prop as any)98 })99 }100 }101 if (immer.onCopy) {102 immer.onCopy(state)103 }10486 // At this point, all descendants of `state.copy` have been finalized,87 // so we can be sure that `scope.canAutoFreeze` is accurate.107 if (immer.autoFreeze && scope.canAutoFreeze_) {88 if (immer.autoFreeze_ && scope.canAutoFreeze_) {89 freeze(state.copy_, false)90 }9192 if (path && scope.patches_) {
81 unmodified lines
174 )175 )176 if (!scope.parent_) maybeFreeze(immer, childValue)177 }197198 if (isDraftProp && immer.onAssign && !isSetMember) {199 immer.onAssign(rootState, prop, childValue)200 }178}179180export function maybeFreeze(immer: Immer, value: any, deep = false) {204 if (immer.autoFreeze && !isDraft(value)) {181 if (immer.autoFreeze_ && !isDraft(value)) {182 freeze(value, deep)183 }184}
modified:src/immerClass.ts+17 added-40 removed
3 unmodified lines
4 markChangedES5,5 IProduceWithPatches,6 IProduce,7 ImmerState,8 each,8 Drafted,9 isDraftable,10 ImmerScope,11 processResult,
18 unmodified lines
30declare const __DEV__: boolean31/* istanbul ignore next */32function verifyMinified() {}3335const configDefaults = {36 useProxies:37 typeof Proxy !== "undefined" &&38 typeof Proxy.revocable !== "undefined" &&39 typeof Reflect !== "undefined",40 autoFreeze: __DEV__41 ? false /* istanbul ignore next */42 : verifyMinified.name === "verifyMinified",43 onAssign: null,44 onDelete: null,45 onCopy: null46} as const4734interface ProducersFns {35 produce: IProduce36 produceWithPatches: IProduceWithPatches37}3839export class Immer implements ProducersFns {54 useProxies: boolean = false55 autoFreeze: boolean = false56 onAssign?: (state: ImmerState, prop: string | number, value: unknown) => void57 onDelete?: (state: ImmerState, prop: string | number) => void58 onCopy?: (state: ImmerState) => void40 useProxies_: boolean =41 typeof Proxy !== "undefined" &&42 typeof Proxy.revocable !== "undefined" &&43 typeof Reflect !== "undefined"44 autoFreeze_: boolean = __DEV__45 ? false /* istanbul ignore next */46 : verifyMinified.name === "verifyMinified"4760 constructor(config?: {61 useProxies?: boolean62 autoFreeze?: boolean63 onAssign?: (64 state: ImmerState,65 prop: string | number,66 value: unknown67 ) => void68 onDelete?: (state: ImmerState, prop: string | number) => void69 onCopy?: (state: ImmerState) => void70 }) {71 each(configDefaults, (key, value) => {72 // @ts-ignore73 this[key] = config?.[key] ?? value74 })75 this.setUseProxies(this.useProxies)48 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {49 if (typeof config?.useProxies === "boolean")50 this.setUseProxies(config!.useProxies)51 if (typeof config?.autoFreeze === "boolean")52 this.setAutoFreeze(config!.autoFreeze)53 this.produce = this.produce.bind(this)54 this.produceWithPatches = this.produceWithPatches.bind(this)55 }56
121 unmodified lines
178 *179 * By default, auto-freezing is disabled in production.180 */181 setAutoFreeze(value: boolean) {205 this.autoFreeze = value182 this.autoFreeze_ = value183 }184185 /**186 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is
1 unmodified line
188 *189 * By default, feature detection is used, so calling this is rarely necessary.190 */191 setUseProxies(value: boolean) {215 this.useProxies = value192 this.useProxies_ = value193 }194195 applyPatches(base: Objectish, patches: Patch[]) {196 // If a patch replaces the entire state, take that replacement as base
27 unmodified lines
224 const draft: Drafted = isMap(value)225 ? proxyMap(value, parent)226 : isSet(value)227 ? proxySet(value, parent)251 : immer.useProxies228 : immer.useProxies_229 ? createProxyProxy(value, parent)230 : createES5Proxy(value, parent)231232 const scope = parent ? parent.scope_ : ImmerScope.current_!
6 unmodified lines
239 scope: ImmerScope,240 thing: any,241 isReplaced: boolean242) {266 if (!immer.useProxies) willFinalizeES5(scope, thing, isReplaced)243 if (!immer.useProxies_) willFinalizeES5(scope, thing, isReplaced)244}245246export function markChanged(immer: Immer, state: ImmerState) {270 if (immer.useProxies) {247 if (immer.useProxies_) {248 markChangedProxy(state)249 } else {250 markChangedES5(state)251 }
1 unmodified line
modified:notes.txt+1 added-1 removed
1 unmodified line
23[x] extract errors4[x] don't enable all features by default5[ ] "undefined" "object" prototype undefined ?6[ ] kill hooks6[x] kill hooks7[x] _ postfix all members8[ ] clean up finalize9[ ] remove die10
41 unmodified lines