class EventLoop {
constructor() {
this.taskQueue = []
}
add(task) {
this.taskQueue.push(task)
}
sleep(d) {
return new Promise(resolve => setTimeout(resolve, d))
}
async run() {
while (true) {
const task = this.taskQueue.shift()
await this.sleep(task.time)
this.execScript(task.code)
this.invokeFunction(task,func)
}
}
}
const eventLoop = new EventLoop()
const macroTaskSet = new Set()
macroTaskSet.add({ time: 1000, code: "let a = 10", func: f })
for (const macroTask of macroTaskSet) {
eventLoop.add(macroTask)
}
eventLoop.run()