Created
July 19, 2022 17:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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