chore: 优化部分逻辑

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2021-08-03 17:30:53 +08:00
parent 586b6acbbc
commit 8a2b0d7c2e
6 changed files with 66 additions and 18 deletions

View File

@ -39,17 +39,29 @@ class Process extends EventEmitter {
return super.on(event, (...args) => {
try {
listener(...args)
} catch (error) {
} catch (origin) {
try {
super.emit('error', error)
super.emit('error', origin)
} catch (error) {
console.ex(origin)
console.ex(error)
}
}
})
}
nextTick(func: Function) {
microTaskPool.execute(func)
nextTick(func: Function, ...args: any[]) {
microTaskPool.execute(() => {
try {
func(args)
} catch (origin) {
try {
super.emit('error', origin)
} catch (error) {
console.ex(origin)
console.ex(error)
}
}
})
}
exit(code: number) {
console.log(`process exit by code ${code}!`)
@ -198,7 +210,7 @@ Object.defineProperty(process, require('core-js/es/symbol/to-string-tag'), { val
const eventLoop = new EventLoop()
Object.defineProperty(process, 'eventLoop', { value: eventLoop })
eventLoop.startEventLoop()
global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {})
global.setGlobal('queueMicrotask', (func: any, ...args: any[]) => process.nextTick(func, args), {})
global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {})
global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {})
global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})