implementation setTimeout&setInterval
This commit is contained in:
@@ -15,6 +15,19 @@ const layout = new VLayout
|
||||
layout.space = 10
|
||||
console.log(layout.viewId)
|
||||
console.log(layout.toModel())
|
||||
log('console', Object.getOwnPropertyNames(console))
|
||||
|
||||
setTimeout(() => {
|
||||
log('exec setTimeout')
|
||||
}, 1000)
|
||||
const timerId = setInterval(() => {
|
||||
log('exec setInterval')
|
||||
}, 1000)
|
||||
|
||||
setTimeout(() => {
|
||||
log('exec cancelTimer')
|
||||
clearInterval(timerId)
|
||||
}, 5000)
|
||||
|
||||
@Registor(context)
|
||||
export class MyPage extends Panel {
|
||||
|
@@ -145,3 +145,44 @@ export function jsCallEntityMethod(contextId: string, methodName: string, args?:
|
||||
}
|
||||
}
|
||||
|
||||
const global = Function('return this')()
|
||||
let __timerId__ = 0
|
||||
const timerCallbacks: Map<number, () => void> = new Map
|
||||
|
||||
declare function nativeSetTimer(timerId: number, interval: number, repeat: boolean): void
|
||||
declare function nativeClearTimer(timerId: number): void
|
||||
|
||||
global.setTimeout = (handler: Function, timeout?: number | undefined, ...args: any[]) => {
|
||||
const id = __timerId__++
|
||||
timerCallbacks.set(id, () => {
|
||||
Reflect.apply(handler, undefined, args)
|
||||
timerCallbacks.delete(id)
|
||||
})
|
||||
nativeSetTimer(id, timeout || 0, false)
|
||||
return id
|
||||
}
|
||||
global.setInterval = (handler: Function, timeout?: number | undefined, ...args: any[]) => {
|
||||
const id = __timerId__++
|
||||
timerCallbacks.set(id, () => {
|
||||
Reflect.apply(handler, undefined, args)
|
||||
})
|
||||
nativeSetTimer(id, timeout || 0, true)
|
||||
return id
|
||||
}
|
||||
|
||||
global.clearTimeout = (timerId: number) => {
|
||||
timerCallbacks.delete(timerId)
|
||||
nativeClearTimer(timerId)
|
||||
}
|
||||
|
||||
global.clearInterval = (timerId: number) => {
|
||||
timerCallbacks.delete(timerId)
|
||||
nativeClearTimer(timerId)
|
||||
}
|
||||
|
||||
export function jsCallbackTimer(timerId: number) {
|
||||
const timerCallback = timerCallbacks.get(timerId)
|
||||
if (timerCallback instanceof Function) {
|
||||
Reflect.apply(timerCallback, undefined, [])
|
||||
}
|
||||
}
|
@@ -16,14 +16,35 @@ function toString(message: any) {
|
||||
}
|
||||
}
|
||||
|
||||
export function log(message: any) {
|
||||
nativeLog('d', toString(message))
|
||||
export function log(...args: any) {
|
||||
let out = ""
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (i > 0) {
|
||||
out += ','
|
||||
}
|
||||
out += toString(arguments[i])
|
||||
}
|
||||
nativeLog('d', out)
|
||||
}
|
||||
|
||||
export function loge(message: any) {
|
||||
nativeLog('e', toString(message))
|
||||
export function loge(...message: any) {
|
||||
let out = ""
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (i > 0) {
|
||||
out += ','
|
||||
}
|
||||
out += toString(arguments[i])
|
||||
}
|
||||
nativeLog('e', out)
|
||||
}
|
||||
|
||||
export function logw(message: any) {
|
||||
nativeLog('w', toString(message))
|
||||
export function logw(...message: any) {
|
||||
let out = ""
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (i > 0) {
|
||||
out += ','
|
||||
}
|
||||
out += toString(arguments[i])
|
||||
}
|
||||
nativeLog('w', out)
|
||||
}
|
||||
|
Reference in New Issue
Block a user