Skip to content

Commit

Permalink
fix(batch): refactor queue to avoid import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandszoke authored and solkimicreb committed Feb 14, 2020
1 parent 2c427cb commit 3cf2180
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { isInsideBatch } from './scheduler'

class Queue {
constructor () {
this.taskSet = new Set()
this.isInsideBatch = false
}

add = task => {
if (isInsideBatch) {
if (this.isInsideBatch) {
this.taskSet.add(task)
} else {
task()
Expand All @@ -19,6 +18,14 @@ class Queue {
this.taskSet.clear()
}
};

on = () => {
this.isInsideBatch = true
};

off = () => {
this.isInsideBatch = false
};
}

export const queue = new Queue()
7 changes: 3 additions & 4 deletions src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { queue } from './queue'

// this runs the passed function and delays all re-renders
// until the function is finished running
export let isInsideBatch = false
export function batch (fn, ctx, args) {
let result
if (isInsideBatch) {
if (queue.isInsideBatch) {
result = fn.apply(ctx, args)
} else {
try {
isInsideBatch = true
queue.on()
result = fn.apply(ctx, args)
} finally {
queue.flush()
isInsideBatch = false
queue.off()
}
}
return result
Expand Down

0 comments on commit 3cf2180

Please sign in to comment.