File tree Expand file tree Collapse file tree 2 files changed +13
-7
lines changed Expand file tree Collapse file tree 2 files changed +13
-7
lines changed Original file line number Diff line number Diff line change 1
- import { isInsideBatch } from './scheduler'
2
-
3
1
class Queue {
4
2
constructor ( ) {
5
3
this . taskSet = new Set ( )
4
+ this . isInsideBatch = false
6
5
}
7
6
8
7
add = task => {
9
- if ( isInsideBatch ) {
8
+ if ( this . isInsideBatch ) {
10
9
this . taskSet . add ( task )
11
10
} else {
12
11
task ( )
@@ -19,6 +18,14 @@ class Queue {
19
18
this . taskSet . clear ( )
20
19
}
21
20
} ;
21
+
22
+ on = ( ) => {
23
+ this . isInsideBatch = true
24
+ } ;
25
+
26
+ off = ( ) => {
27
+ this . isInsideBatch = false
28
+ } ;
22
29
}
23
30
24
31
export const queue = new Queue ( )
Original file line number Diff line number Diff line change @@ -5,18 +5,17 @@ import { queue } from './queue'
5
5
6
6
// this runs the passed function and delays all re-renders
7
7
// until the function is finished running
8
- export let isInsideBatch = false
9
8
export function batch ( fn , ctx , args ) {
10
9
let result
11
- if ( isInsideBatch ) {
10
+ if ( queue . isInsideBatch ) {
12
11
result = fn . apply ( ctx , args )
13
12
} else {
14
13
try {
15
- isInsideBatch = true
14
+ queue . on ( )
16
15
result = fn . apply ( ctx , args )
17
16
} finally {
18
17
queue . flush ( )
19
- isInsideBatch = false
18
+ queue . off ( )
20
19
}
21
20
}
22
21
return result
You can’t perform that action at this time.
0 commit comments