Skip to content

Commit 29347d2

Browse files
authored
Merge pull request #1 from final-form/used-batch
Used batch
2 parents a9c2d68 + 1189f92 commit 29347d2

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "final-form-calculate",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description":
55
"Decorator for calculating field values based on other field values in 🏁 Final Form",
66
"main": "dist/final-form-calculate.cjs.js",

src/decorator.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@ const createDecorator = (...calculations: Calculation[]): Decorator => (
99
let previousValues = {}
1010
const unsubscribe = form.subscribe(
1111
({ values }) => {
12-
const runUpdates = (field: string, updates: Updates) => {
13-
const next = getIn(values, field)
14-
const previous = getIn(previousValues, field)
15-
if (next !== previous) {
16-
Object.keys(updates).forEach(destField => {
17-
const update = updates[destField]
18-
form.change(destField, update(next, values))
19-
})
20-
}
21-
}
22-
const fields = form.getRegisteredFields()
23-
calculations.forEach(({ field, updates }) => {
24-
if (typeof field === 'string') {
25-
runUpdates(field, updates)
26-
} else {
27-
// field is a regex
28-
const regex = (field: RegExp)
29-
fields.forEach(fieldName => {
30-
if (regex.test(fieldName)) {
31-
runUpdates(fieldName, updates)
32-
}
33-
})
12+
form.batch(() => {
13+
const runUpdates = (field: string, updates: Updates) => {
14+
const next = getIn(values, field)
15+
const previous = getIn(previousValues, field)
16+
if (next !== previous) {
17+
Object.keys(updates).forEach(destField => {
18+
const update = updates[destField]
19+
form.change(destField, update(next, values))
20+
})
21+
}
3422
}
23+
const fields = form.getRegisteredFields()
24+
calculations.forEach(({ field, updates }) => {
25+
if (typeof field === 'string') {
26+
runUpdates(field, updates)
27+
} else {
28+
// field is a regex
29+
const regex = (field: RegExp)
30+
fields.forEach(fieldName => {
31+
if (regex.test(fieldName)) {
32+
runUpdates(fieldName, updates)
33+
}
34+
})
35+
}
36+
})
37+
previousValues = values
3538
})
36-
previousValues = values
3739
},
3840
{ values: true }
3941
)

src/decorator.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('decorator', () => {
178178
form.change('items[0]', 3)
179179

180180
expect(sum).toHaveBeenCalled()
181-
expect(sum).toHaveBeenCalledTimes(2)
181+
expect(sum).toHaveBeenCalledTimes(1)
182182

183183
expect(spy).toHaveBeenCalledTimes(3)
184184
expect(spy.mock.calls[1][0].values).toEqual({ items: [3] })
@@ -188,7 +188,7 @@ describe('decorator', () => {
188188
form.change('items[1]', 4)
189189

190190
expect(sum).toHaveBeenCalled()
191-
expect(sum).toHaveBeenCalledTimes(4)
191+
expect(sum).toHaveBeenCalledTimes(2)
192192

193193
expect(spy).toHaveBeenCalledTimes(5)
194194
expect(spy.mock.calls[3][0].values).toEqual({ items: [3, 4], total: 3 })
@@ -198,7 +198,7 @@ describe('decorator', () => {
198198
form.change('items[2]', 5)
199199

200200
expect(sum).toHaveBeenCalled()
201-
expect(sum).toHaveBeenCalledTimes(6)
201+
expect(sum).toHaveBeenCalledTimes(3)
202202

203203
expect(spy).toHaveBeenCalledTimes(7)
204204
expect(spy.mock.calls[5][0].values).toEqual({ items: [3, 4, 5], total: 7 })

0 commit comments

Comments
 (0)