Skip to content

Commit 94696e1

Browse files
committed
Fixed mobxjs#1739 for proxied objects: delete and in don't respect property deletions
1 parent 6836b77 commit 94696e1

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 5.5.0 / 4.5.0
2+
3+
(Minor version of `5` was bumped significantly to make the number better correlate together :-))
4+
5+
* `has` now returns true for computed fields
6+
* `get` now returns a value for computed fields
7+
* `has` now picks up `removed` / `delete`-ed fields (#1739)
8+
19
# 5.1.2 / 4.4.2
210

311
* Fixed [#1650](https://github.com/mobxjs/mobx/issues/1650), decorating fields with the name `toString` does not behave correctly.

src/types/dynamicobject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const objectProxyTraps: ProxyHandler<any> = {
1919
if (name === $mobx || name === "constructor" || name === mobxDidRunLazyInitializersSymbol)
2020
return true
2121
const adm = getAdm(target)
22-
if (adm.values.get(name as string)) return true
22+
// MWE: should `in` operator be reactive? If not, below code path will be faster / more memory efficient
23+
// TODO: check performance stats!
24+
// if (adm.values.get(name as string)) return true
2325
if (typeof name === "string") return adm.has(name)
2426
return (name as any) in target
2527
},

test/base/object-api-proxy.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const mobx = require("../../src/mobx")
22
const {
3+
has,
4+
autorun,
35
when,
46
set,
57
remove,
@@ -148,7 +150,7 @@ test("has and get are reactive", async () => {
148150
await p2
149151
})
150152

151-
test("computed props are not considered part of collections", () => {
153+
test("computed props are considered part of collections", () => {
152154
const x = observable({
153155
get y() {
154156
return 3
@@ -161,3 +163,21 @@ test("computed props are not considered part of collections", () => {
161163
expect(values(x)).toEqual([])
162164
expect(entries(x)).toEqual([])
163165
})
166+
167+
test("#1739 - delete and undelete should work", () => {
168+
const x = observable({})
169+
170+
const events = []
171+
autorun(() => {
172+
// events.push("a" in x)
173+
events.push(has(x, "a"))
174+
})
175+
176+
x.a = 1
177+
x.a++
178+
delete x.a
179+
x.a = 5
180+
delete x.a
181+
x.a = 5
182+
expect(events).toEqual([false, true, false, true, false, true])
183+
})

test/base/proxies.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ test("in operator", () => {
146146
return 5
147147
}
148148
})
149-
expect("a" in x).toBeTruthy()
150-
expect("b" in x).toBeTruthy()
151149
expect("x" in x).toBeTruthy()
152150
expect("y" in x).toBeTruthy()
151+
expect("a" in x).toBeTruthy()
152+
expect("b" in x).toBeTruthy()
153153
expect("z" in x).toBeTruthy()
154154
expect("c" in x).toBeFalsy()
155155
expect("c" in x).toBeFalsy() // not accidentally create

0 commit comments

Comments
 (0)