Skip to content

Commit 3cf2180

Browse files
rolandszokesolkimicreb
authored andcommitted
fix(batch): refactor queue to avoid import cycle
1 parent 2c427cb commit 3cf2180

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/queue.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { isInsideBatch } from './scheduler'
2-
31
class Queue {
42
constructor () {
53
this.taskSet = new Set()
4+
this.isInsideBatch = false
65
}
76

87
add = task => {
9-
if (isInsideBatch) {
8+
if (this.isInsideBatch) {
109
this.taskSet.add(task)
1110
} else {
1211
task()
@@ -19,6 +18,14 @@ class Queue {
1918
this.taskSet.clear()
2019
}
2120
};
21+
22+
on = () => {
23+
this.isInsideBatch = true
24+
};
25+
26+
off = () => {
27+
this.isInsideBatch = false
28+
};
2229
}
2330

2431
export const queue = new Queue()

src/scheduler.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import { queue } from './queue'
55

66
// this runs the passed function and delays all re-renders
77
// until the function is finished running
8-
export let isInsideBatch = false
98
export function batch (fn, ctx, args) {
109
let result
11-
if (isInsideBatch) {
10+
if (queue.isInsideBatch) {
1211
result = fn.apply(ctx, args)
1312
} else {
1413
try {
15-
isInsideBatch = true
14+
queue.on()
1615
result = fn.apply(ctx, args)
1716
} finally {
1817
queue.flush()
19-
isInsideBatch = false
18+
queue.off()
2019
}
2120
}
2221
return result

0 commit comments

Comments
 (0)