Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/reactive/src/__tests__/autorun.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,19 @@ test('reaction recollect dependencies', () => {
expect(fn2).toBeCalledTimes(2)
expect(trigger2).toBeCalledTimes(2)
})

test('avoid unnecessary reaction', () => {
const obs = observable<any>({
aa: { v: 1 },
})
const fn1 = jest.fn()

reaction(() => {
fn1()
return obs.aa.v
})

obs.aa = obs.aa

expect(fn1).toBeCalledTimes(1)
})
7 changes: 6 additions & 1 deletion packages/reactive/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ export const baseHandlers: ProxyHandler<any> = {
}
const hadKey = hasOwnProperty.call(target, key)
const newValue = createObservable(target, key, value)
const oldValue = target[key]

// If the old value has already generated an observable result,
// directly use observableResult compapre to value to avoid unnecessary reactions
const observableResult = RawProxy.get(target[key])
const oldValue = observableResult ? observableResult : target[key]

target[key] = newValue // use Reflect.set is too slow
if (!hadKey) {
runReactionsFromTargetKey({
Expand Down
Loading