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

@ -18,12 +18,12 @@ export class MyPage extends Panel {
log("Hello.HEGO") log("Hello.HEGO")
logw("Hello.HEGO") logw("Hello.HEGO")
loge("Hello.HEGO") loge("Hello.HEGO")
context.bridge.demo_testPromise(true).then((r) => { context.demo.testPromise(true).then((r) => {
log('resolve', r) log('resolve', r)
}, (e) => { }, (e) => {
log('reject', e) log('reject', e)
}) })
context.bridge.demo_testPromise(false).then((r) => { context.demo.testPromise(false).then((r) => {
log('resolve', r) log('resolve', r)
}, (e) => { }, (e) => {
log('reject', e) log('reject', e)

View File

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

View File

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

View File

@ -2,7 +2,6 @@ import './../runtime/global'
import { View, Stack, Group } from "./view"; import { View, Stack, Group } from "./view";
import { loge, log } from '../util/log'; import { loge, log } from '../util/log';
import { Model } from '../util/types'; import { Model } from '../util/types';
import { Context } from '../runtime/sandbox';
export function NativeCall(target: Panel, propertyKey: string, descriptor: PropertyDescriptor) { export function NativeCall(target: Panel, propertyKey: string, descriptor: PropertyDescriptor) {
@ -17,7 +16,7 @@ export function NativeCall(target: Panel, propertyKey: string, descriptor: Prope
type Frame = { width: number, height: number } type Frame = { width: number, height: number }
export abstract class Panel { export abstract class Panel {
context?: Context context?: any
onCreate() { } onCreate() { }
onDestory() { } onDestory() { }
onShow() { } onShow() { }
@ -93,7 +92,7 @@ export abstract class Panel {
private nativeRender(model: Model) { private nativeRender(model: Model) {
if (this.context) { if (this.context) {
this.context.bridge.shader_render(model) this.context.shader.render(model)
} }
} }