diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 4756165a..f6fa32d7 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -18,12 +18,12 @@ export class MyPage extends Panel { log("Hello.HEGO") logw("Hello.HEGO") loge("Hello.HEGO") - context.bridge.demo_testPromise(true).then((r) => { + context.demo.testPromise(true).then((r) => { log('resolve', r) }, (e) => { log('reject', e) }) - context.bridge.demo_testPromise(false).then((r) => { + context.demo.testPromise(false).then((r) => { log('resolve', r) }, (e) => { log('reject', e) diff --git a/js-framework/src/runtime/global.ts b/js-framework/src/runtime/global.ts index ca77a600..f2e4a59c 100644 --- a/js-framework/src/runtime/global.ts +++ b/js-framework/src/runtime/global.ts @@ -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 } }; function Entry(constructor: { new(...args: any[]): {} }): any } export { } \ No newline at end of file diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index e4d7b7f3..b3fdac99 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -102,7 +102,6 @@ export class Context { entity: any id: string callbacks: Map = new Map - bridge: { [index: string]: (args?: any) => Promise } 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 { const callbackId = uniqueId('callback') - nativeBridge(this.id, namespace, method, callbackId, args) return new Promise((resolve, reject) => { this.callbacks.set(callbackId, { diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index cbe3f372..592c71f5 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -2,7 +2,6 @@ import './../runtime/global' import { View, Stack, Group } from "./view"; import { loge, log } from '../util/log'; import { Model } from '../util/types'; -import { Context } from '../runtime/sandbox'; 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 } export abstract class Panel { - context?: Context + context?: any onCreate() { } onDestory() { } onShow() { } @@ -93,7 +92,7 @@ export abstract class Panel { private nativeRender(model: Model) { if (this.context) { - this.context.bridge.shader_render(model) + this.context.shader.render(model) } }