Skip to content

Instantly share code, notes, and snippets.

@zim32
Created July 19, 2022 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zim32/01d712d1606137efc603876cf7bc45f5 to your computer and use it in GitHub Desktop.
Save zim32/01d712d1606137efc603876cf7bc45f5 to your computer and use it in GitHub Desktop.
Vue js hook to queue callbacks and execute then one by one, one callback in one tick
const queue = []
let canExecuteThisTick = true
import {nextTick} from 'vue'
export function useTickSequence() {
const processQueue = () => {
if (canExecuteThisTick) {
canExecuteThisTick = false
}
const callback = queue.shift()
callback()
nextTick(() => {
canExecuteThisTick = true
if (queue.length !== 0) {
processQueue()
}
})
}
const enqueue = callback => {
queue.push(callback)
if (canExecuteThisTick) {
processQueue()
}
}
return {
enqueue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment