Skip to content

Commit

Permalink
Make it possible to store other non-numeric keys as well, while we ar…
Browse files Browse the repository at this point in the history
…e at it.
  • Loading branch information
mweststrate committed Jul 17, 2019
1 parent 5ff67e8 commit 9302c44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/types/observablearray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,17 @@ const arrayTraps = {
set(target, name, value): boolean {
if (name === "length") {
target[$mobx].setArrayLength(value)
return true
}
if (typeof name === "number") {
arrayExtensions.set.call(target, name, value)
return true
}
if (typeof name === "symbol") {
if (typeof name === "symbol" || isNaN(name)) {
target[name] = value
return true
}
if (!isNaN(name)) {
} else {
// numeric string
arrayExtensions.set.call(target, parseInt(name), value)
return true
}
return false
return true
},
preventExtensions(target) {
fail(`Observable arrays cannot be frozen`)
Expand Down
22 changes: 22 additions & 0 deletions test/base/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,25 @@ test("#2044 symbol key on array", () => {
expect(reacted).toBe(false)
d()
})

test("#2044 non-symbol key on array", () => {
const x = observable([1, 2])
const s = "test"
x[s] = 3
expect(x[s]).toBe(3)

let reacted = false
const d = reaction(
() => x[s],
() => {
reacted = true
}
)

x[s] = 4
expect(x[s]).toBe(4)

// although x[s] can be stored, it won't be reactive!
expect(reacted).toBe(false)
d()
})

0 comments on commit 9302c44

Please sign in to comment.