forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested.js
More file actions
88 lines (74 loc) · 2.04 KB
/
Copy pathnested.js
File metadata and controls
88 lines (74 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict"
import { extendObservable, observable, autorun, computed, runInAction } from "../../src/mobx.ts"
test("nested computeds should not run unnecessary", () => {
function Item(name) {
extendObservable(this, {
name: name,
get index() {
const i = store.items.indexOf(this)
if (i === -1) throw "not found"
return i
}
})
}
const store = observable({
items: [],
get asString() {
return this.items.map(item => item.index + ":" + item.name).join(",")
}
})
store.items.push(new Item("item1"))
const values = []
autorun(() => {
values.push(store.asString)
})
store.items.replace([new Item("item2")])
expect(values).toEqual(["0:item1", "0:item2"])
})
test("fix #1535: stale observables", cb => {
// see https://codesandbox.io/s/k92o2jmz63
const snapshots = []
const x = observable.box(1)
// Depends on observable x
const derived1 = computed(() => {
return x.get() + 1
})
// Depends on computed derived1
const derived2 = computed(() => {
return derived1.get() + 1
})
function increment() {
runInAction(() => {
x.set(x.get() + 1)
// No problems here
derived1.get()
derived2.get()
})
}
function brokenIncrement() {
runInAction(() => x.set(x.get() + 1))
// Acessing computed outside of action causes staleness
// NOTE IT DOESN'T MATTER WHICH COMPUTED IS ACCESSED
// derived1.get();
debugger
derived2.get()
}
autorun(
() => {
snapshots.push(`${x.get()}, ${derived1.get()}, ${derived2.get()}`)
},
{
scheduler(f) {
setImmediate(f)
}
}
)
increment()
setTimeout(() => {
brokenIncrement()
}, 100)
setTimeout(() => {
expect(snapshots).toEqual(["2, 3, 4", "3, 4, 5"])
cb()
}, 1000)
})