refactor(core): core task function
move event loop task to @ccms/micro-task thread BREAKING CHANGE: setTimeout & setInterval no longer run on main thread Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
@ -5,6 +5,7 @@ const ThreadGroup = Java.type("java.lang.ThreadGroup")
|
||||
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
|
||||
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
|
||||
const LinkedBlockingQueue = Java.type("java.util.concurrent.LinkedBlockingQueue")
|
||||
const TimeUnit = Java.type("java.util.concurrent.TimeUnit")
|
||||
|
||||
const threadCount = new AtomicInteger(0)
|
||||
const threadGroup = new ThreadGroup("@ccms/ployfill-micro-task")
|
||||
@ -14,7 +15,6 @@ const microTaskPool = new ThreadPoolExecutor(
|
||||
(run: any) => new Thread(threadGroup, run, "@ccms/micro-task-" + threadCount.incrementAndGet()),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy()
|
||||
)
|
||||
|
||||
class Process extends EventEmitter {
|
||||
env = {
|
||||
__noSuchProperty__: (prop) => {
|
||||
@ -44,7 +44,46 @@ class Process extends EventEmitter {
|
||||
exit(code: number) {
|
||||
process.emit('exit', code)
|
||||
microTaskPool.shutdown()
|
||||
console.log('await microTaskPool termination...')
|
||||
microTaskPool.awaitTermination(5000, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
}
|
||||
const timeoutCount = new AtomicInteger(0)
|
||||
const timeoutTasks = []
|
||||
function setTimeout(func: Function, time: number, ...args: any[]) {
|
||||
let taskId = timeoutCount.incrementAndGet()
|
||||
timeoutTasks[taskId] = func
|
||||
process.nextTick(() => {
|
||||
Thread.sleep(time)
|
||||
if (timeoutTasks[taskId]) { func(...args) }
|
||||
})
|
||||
return taskId
|
||||
}
|
||||
function clearTimeout(taskId: number) {
|
||||
delete timeoutTasks[taskId]
|
||||
}
|
||||
const intervalCount = new AtomicInteger(0)
|
||||
const intervalTasks = []
|
||||
function setInterval(func: Function, time: number, ...args: any[]) {
|
||||
let taskId = intervalCount.incrementAndGet()
|
||||
intervalTasks[taskId] = func
|
||||
process.nextTick(() => {
|
||||
Thread.sleep(time)
|
||||
while (intervalTasks[taskId]) {
|
||||
func(...args)
|
||||
Thread.sleep(time)
|
||||
}
|
||||
})
|
||||
return taskId
|
||||
}
|
||||
function clearInterval(taskId: number) {
|
||||
delete intervalTasks[taskId]
|
||||
}
|
||||
global.setGlobal('process', new Process(), {})
|
||||
global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {})
|
||||
global.setGlobal('setTimeout', setTimeout, {})
|
||||
global.setGlobal('clearTimeout', clearTimeout, {})
|
||||
global.setGlobal('setImmediate', (func: Function, ...args: any[]) => setTimeout(func, 0, ...args), {})
|
||||
global.setGlobal('clearImmediate ', clearTimeout, {})
|
||||
global.setGlobal('setInterval', setInterval, {})
|
||||
global.setGlobal('clearInterval', clearInterval, {})
|
||||
|
Reference in New Issue
Block a user