-
Notifications
You must be signed in to change notification settings - Fork 9
/
batchMutations.js
44 lines (33 loc) · 949 Bytes
/
batchMutations.js
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
// @flow
import fastdom from 'fastdom'
type id = Function | null
// Style updates
let writeId = null
// Style reads
let readId = null
// Scheduled jobs are stored in the queues which are emptied at the turn of next frame using rAF. This helps in reducing recalcs/sec and speed up the animation performance.
// Batch style mutations
export const batchMutation = (mutation: Function): id => {
writeId = fastdom.mutate(() => {
return mutation()
})
return writeId
}
// Batch style reads
export const batchRead = (reads: Function): id => {
readId = fastdom.measure(() => {
return reads()
})
return readId
}
// In case we don't have the current node on which the mutations were applied, catch the exceptions.
export const exceptions = () => {
fastdom.catch = error => {
console.error(error)
}
}
// Clear the scheduled jobs
export const emptyScheduledJobs = () => {
fastdom.clear(readId)
fastdom.clear(writeId)
}