Skip to content

Commit

Permalink
fix(reactive): fix unexpect effect in reactions (#2563)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Dec 3, 2021
1 parent 35a18c4 commit 8f8db67
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createForm } from '../'
import { DataField } from '../types'
import { attach, sleep } from './shared'

test('create field', () => {
Expand Down Expand Up @@ -1810,3 +1811,26 @@ test('custom validator to get ctx.field', async () => {
expect(!!ctxField).toBeTruthy()
expect(!!ctxForm).toBeTruthy()
})

test('single direction linkage effect', async () => {
const form = attach(createForm())

const input1 = form.createField({
name: 'input1',
reactions: (field: DataField) => {
if (!field.selfModified) {
return
}
input2.value = field.value
},
})

const input2 = form.createField({
name: 'input2',
})

await input1.onInput('123')
expect(input2.value).toBe('123')
await input2.onInput('321')
expect(input2.value).toBe('321')
})
4 changes: 4 additions & 0 deletions packages/reactive/src/autorun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
releaseBindingReactions,
disposeEffects,
hasDepsChange,
untrackStart,
untrackEnd,
} from './reaction'
import { isFn } from './checkers'
import { ReactionStack } from './environment'
Expand Down Expand Up @@ -117,10 +119,12 @@ export const reaction = <T>(

const fireAction = () => {
try {
untrackStart()
batchStart()
if (isFn(subscriber)) subscriber(value.currentValue, value.oldValue)
} finally {
batchEnd()
untrackEnd()
}
}

Expand Down

0 comments on commit 8f8db67

Please sign in to comment.