From 3a6b0725434a85197e9b6f45a3ab65ac99c167d7 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 6 Nov 2018 15:30:10 +0100 Subject: [PATCH] Fixed #1796, detect undelete of undefined property --- src/types/dynamicobject.ts | 11 ++++++++++- test/base/proxies.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/types/dynamicobject.ts b/src/types/dynamicobject.ts index 884240cfd..2569d7806 100644 --- a/src/types/dynamicobject.ts +++ b/src/types/dynamicobject.ts @@ -30,7 +30,16 @@ const objectProxyTraps: ProxyHandler = { return target[name] const adm = getAdm(target) const observable = adm.values.get(name as string) - if (observable instanceof Atom) return (observable as any).get() + if (observable instanceof Atom) { + const result = (observable as any).get() + if (result === undefined) { + // This fixes #1796, because deleting a prop that has an + // undefined value won't retrigger a observer (no visible effect), + // the autorun wouldn't subscribe to future key changes (see also next comment) + adm.has(name as any) + } + return result + } // make sure we start listening to future keys // note that we only do this here for optimization if (typeof name === "string") adm.has(name) diff --git a/test/base/proxies.js b/test/base/proxies.js index 7a4bdf378..74542ed13 100644 --- a/test/base/proxies.js +++ b/test/base/proxies.js @@ -371,3 +371,22 @@ test("predictable 'this' - 2", () => { expect(a.a2()).toBe(a) expect(a.computed).toBe(a) }) + +test("1796 - deleting / recreate prop", () => { + let value = observable({ + foo: undefined // if foo is something like 'abc', it works. + }) + + const events = [] + + const aut = autorun(() => { + events.push(value.foo) + }) + delete value.foo + value.foo = "def" + expect(events).toEqual([ + undefined, + undefined, // ideally not, but ok.. + "def" + ]) +})