hugoWebsite/node_modules/alpinejs/src/nextTick.js

30 lines
470 B
JavaScript
Raw Normal View History

2022-10-29 00:20:35 +02:00
let tickStack = []
let isHolding = false
export function nextTick(callback = () => {}) {
queueMicrotask(() => {
isHolding || setTimeout(() => {
releaseNextTicks()
})
})
return new Promise((res) => {
tickStack.push(() => {
callback();
res();
});
})
}
export function releaseNextTicks() {
isHolding = false
while (tickStack.length) tickStack.shift()()
}
export function holdNextTicks() {
isHolding = true
}