add context.bridge

This commit is contained in:
pengfei.zhou
2019-07-20 13:40:06 +08:00
parent 16732340c7
commit ef5bb17380
5 changed files with 34 additions and 15 deletions

View File

@@ -19,7 +19,7 @@ log('console', Object.getOwnPropertyNames(console))
setTimeout(() => {
log('exec setTimeout')
context.callNative("modal", "toast", "Hello,Doric!")
// context.callNative("modal", "toast", "Hello,Doric!")
}, 1000)
const timerId = setInterval(() => {
log('exec setInterval')
@@ -39,5 +39,8 @@ export class MyPage extends Panel {
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
setTimeout(() => {
context.bridge.demo_test()
}, 1000)
}
}

View File

@@ -71,8 +71,22 @@ export class Context {
entity: any
id: string
callbacks: Map<string, { resolve: Function, reject: Function }> = new Map
bridge: { [index: string]: (args?: any) => Promise<any> }
constructor(id: string) {
this.id = id
this.bridge = new Proxy({}, {
get: (target, p: any) => {
if (typeof p === 'string') {
return (args?: any) => {
const array = p.split('_')
return this.callNative(array[0], array[1], args)
}
} else {
return Reflect.get(target, p)
}
}
})
}
callNative(namespace: string, method: string, args?: any): Promise<any> {
const callbackId = uniqueId('callback')