change bridge call function

This commit is contained in:
pengfei.zhou
2019-07-22 15:02:38 +08:00
parent b73312d1e4
commit ed8b221288
4 changed files with 27 additions and 16 deletions

View File

@@ -1,8 +1,7 @@
import { Context } from "./sandbox";
export * from 'reflect-metadata'
declare global {
const context: Context;
const context: { [index: string]: { [index: string]: (args?: any) => Promise<any> } };
function Entry(constructor: { new(...args: any[]): {} }): any
}
export { }

View File

@@ -102,7 +102,6 @@ export class Context {
entity: any
id: string
callbacks: Map<string, { resolve: Function, reject: Function }> = new Map
bridge: { [index: string]: (args?: any) => Promise<any> }
hookBeforeNativeCall() {
if (this.entity && Reflect.has(this.entity, 'hookBeforeNativeCall')) {
@@ -118,22 +117,36 @@ export class Context {
constructor(id: string) {
this.id = id
this.bridge = new Proxy({}, {
return new Proxy(this, {
get: (target, p: any) => {
if (typeof p === 'string') {
return (args?: any) => {
const array = p.split('_')
return this.callNative(array[0], array[1], args)
}
} else {
if (Reflect.has(target, p)) {
return Reflect.get(target, p)
} else {
const namespace = p
return new Proxy({}, {
get: (target, p: any) => {
if (Reflect.has(target, p)) {
return Reflect.get(target, p)
} else {
const context = this
return function () {
const args = []
args.push(namespace)
args.push(p)
for (let arg of arguments) {
args.push(arg)
}
return Reflect.apply(context.callNative, context, args)
}
}
}
})
}
}
})
}
callNative(namespace: string, method: string, args?: any): Promise<any> {
const callbackId = uniqueId('callback')
nativeBridge(this.id, namespace, method, callbackId, args)
return new Promise((resolve, reject) => {
this.callbacks.set(callbackId, {