52 lines
927 B
TypeScript
Raw Normal View History

2018-03-14 03:28:31 +00:00
const timeCapacity = 6.5
2017-07-05 19:06:38 +00:00
export class MutationQueue {
2018-03-14 02:08:50 +00:00
mutations: Array<() => void>
onClearCallBack: () => void
constructor() {
this.mutations = []
}
queue(mutation: () => void) {
this.mutations.push(mutation)
if(this.mutations.length === 1) {
window.requestAnimationFrame(() => this.mutateAll())
}
}
mutateAll() {
let start = performance.now()
for(let i = 0; i < this.mutations.length; i++) {
2018-03-14 03:28:31 +00:00
if(performance.now() - start > timeCapacity) {
2018-03-14 02:08:50 +00:00
let end = performance.now()
this.mutations = this.mutations.slice(i)
window.requestAnimationFrame(() => this.mutateAll())
return
}
2018-03-16 18:39:48 +00:00
try {
this.mutations[i]()
} catch(err) {
console.error(err)
}
2018-03-14 02:08:50 +00:00
}
this.clear()
}
clear() {
this.mutations.length = 0
if(this.onClearCallBack) {
this.onClearCallBack()
this.onClearCallBack = null
}
}
wait(callBack: () => void) {
this.onClearCallBack = callBack
}
2017-07-05 19:06:38 +00:00
}