change bridge call function
This commit is contained in:
parent
b73312d1e4
commit
ed8b221288
@ -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)
|
||||
|
@ -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 { }
|
@ -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, {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user