Skip to content

Commit 3a6b072

Browse files
committed
Fixed mobxjs#1796, detect undelete of undefined property
1 parent 4a274fa commit 3a6b072

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎src/types/dynamicobject.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ const objectProxyTraps: ProxyHandler<any> = {
3030
return target[name]
3131
const adm = getAdm(target)
3232
const observable = adm.values.get(name as string)
33-
if (observable instanceof Atom) return (observable as any).get()
33+
if (observable instanceof Atom) {
34+
const result = (observable as any).get()
35+
if (result === undefined) {
36+
// This fixes #1796, because deleting a prop that has an
37+
// undefined value won't retrigger a observer (no visible effect),
38+
// the autorun wouldn't subscribe to future key changes (see also next comment)
39+
adm.has(name as any)
40+
}
41+
return result
42+
}
3443
// make sure we start listening to future keys
3544
// note that we only do this here for optimization
3645
if (typeof name === "string") adm.has(name)

‎test/base/proxies.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,22 @@ test("predictable 'this' - 2", () => {
371371
expect(a.a2()).toBe(a)
372372
expect(a.computed).toBe(a)
373373
})
374+
375+
test("1796 - deleting / recreate prop", () => {
376+
let value = observable({
377+
foo: undefined // if foo is something like 'abc', it works.
378+
})
379+
380+
const events = []
381+
382+
const aut = autorun(() => {
383+
events.push(value.foo)
384+
})
385+
delete value.foo
386+
value.foo = "def"
387+
expect(events).toEqual([
388+
undefined,
389+
undefined, // ideally not, but ok..
390+
"def"
391+
])
392+
})

0 commit comments

Comments
 (0)