Skip to content

Commit 73d5c7d

Browse files
committed
fix(reactive): fix computed/tracker did not recollect dependencies
1 parent fb217f2 commit 73d5c7d

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

packages/reactive/src/__tests__/annotations.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,26 @@ test('computed with computed array length', () => {
248248
obs.arr = []
249249
expect(handler).toBeCalledWith(false)
250250
})
251+
252+
test('computed recollect dependencies', () => {
253+
const computed = jest.fn()
254+
const obs = model({
255+
aa: 'aaa',
256+
bb: 'bbb',
257+
cc: 'ccc',
258+
get compute() {
259+
computed()
260+
if (this.aa === 'aaa') {
261+
return this.bb
262+
}
263+
return this.cc
264+
},
265+
})
266+
const handler = jest.fn()
267+
autorun(() => {
268+
handler(obs.compute)
269+
})
270+
obs.aa = '111'
271+
obs.bb = '222'
272+
expect(computed).toBeCalledTimes(2)
273+
})

packages/reactive/src/__tests__/autorun.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,60 @@ test('atom mutate value by computed depend', () => {
563563
expect(handler).toBeCalledWith(undefined, undefined)
564564
expect(handler).toBeCalledWith(123, 321)
565565
})
566+
567+
test('autorun recollect dependencies', () => {
568+
const obs = observable<any>({
569+
aa: 'aaa',
570+
bb: 'bbb',
571+
cc: 'ccc',
572+
})
573+
const fn = jest.fn()
574+
autorun(() => {
575+
fn()
576+
if (obs.aa === 'aaa') {
577+
return obs.bb
578+
}
579+
return obs.cc
580+
})
581+
obs.aa = '111'
582+
obs.bb = '222'
583+
expect(fn).toBeCalledTimes(2)
584+
})
585+
586+
test('reaction recollect dependencies', () => {
587+
const obs = observable<any>({
588+
aa: 'aaa',
589+
bb: 'bbb',
590+
cc: 'ccc',
591+
})
592+
const fn1 = jest.fn()
593+
const fn2 = jest.fn()
594+
const trigger1 = jest.fn()
595+
const trigger2 = jest.fn()
596+
reaction(() => {
597+
fn1()
598+
if (obs.aa === 'aaa') {
599+
return obs.bb
600+
}
601+
return obs.cc
602+
}, trigger1)
603+
reaction(
604+
() => {
605+
fn2()
606+
if (obs.aa === 'aaa') {
607+
return obs.bb
608+
}
609+
return obs.cc
610+
},
611+
trigger2,
612+
{
613+
fireImmediately: true,
614+
}
615+
)
616+
obs.aa = '111'
617+
obs.bb = '222'
618+
expect(fn1).toBeCalledTimes(2)
619+
expect(trigger1).toBeCalledTimes(1)
620+
expect(fn2).toBeCalledTimes(2)
621+
expect(trigger2).toBeCalledTimes(2)
622+
})

packages/reactive/src/__tests__/tracker.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,29 @@ test('nested tracker', () => {
3737
expect(fn).toBeCalledTimes(2)
3838
tracker.dispose()
3939
})
40+
41+
test('tracker recollect dependencies', () => {
42+
const obs = observable<any>({
43+
aa: 'aaa',
44+
bb: 'bbb',
45+
cc: 'ccc',
46+
})
47+
const fn = jest.fn()
48+
const view = () => {
49+
fn()
50+
if (obs.aa === 'aaa') {
51+
return obs.bb
52+
}
53+
return obs.cc
54+
}
55+
const handler = () => {
56+
tracker.track(view)
57+
}
58+
const tracker = new Tracker(handler)
59+
60+
tracker.track(view)
61+
obs.aa = '111'
62+
obs.bb = '222'
63+
expect(fn).toBeCalledTimes(2)
64+
tracker.dispose()
65+
})

packages/reactive/src/annotations/computed.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isUntracking,
1010
batchStart,
1111
batchEnd,
12+
releaseBindingReactions,
1213
} from '../reaction'
1314

1415
interface IValue<T = any> {
@@ -55,6 +56,7 @@ export const computed: IComputed = createAnnotation(
5556
}
5657
function reaction() {
5758
if (ReactionStack.indexOf(reaction) === -1) {
59+
releaseBindingReactions(reaction)
5860
try {
5961
ReactionStack.push(reaction)
6062
compute()

packages/reactive/src/tracker.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { ReactionStack } from './environment'
22
import { isFn } from './checkers'
33
import { Reaction } from './types'
4-
import { batchEnd, batchStart, disposeBindingReactions } from './reaction'
4+
import {
5+
batchEnd,
6+
batchStart,
7+
disposeBindingReactions,
8+
releaseBindingReactions,
9+
} from './reaction'
510

611
export class Tracker {
712
private results: any
@@ -21,6 +26,7 @@ export class Tracker {
2126
if (!isFn(tracker)) return this.results
2227
if (this.track._boundary > 0) return
2328
if (ReactionStack.indexOf(this.track) === -1) {
29+
releaseBindingReactions(this.track)
2430
try {
2531
batchStart()
2632
ReactionStack.push(this.track)

0 commit comments

Comments
 (0)