diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index 39b449fc..61c88a97 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -1,4 +1,5 @@ import { uniqueId } from "../util/uniqueId"; +import { loge } from "../util/log"; /** * ``` TypeScript @@ -27,19 +28,6 @@ declare function nativeRequire(moduleName: string): boolean declare function nativeBridge(contextId: string, namespace: string, method: string, args?: any, callbackId?: string): boolean - -export function log(message: any) { - console.log(message) -} - -export function loge(message: any) { - console.error(message) -} - -export function logw(message: any) { - console.warn(message) -} - export function jsCallResolve(contextId: string, callbackId: string, args?: any) { const context = gContexts.get(contextId) if (context === undefined) { diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index e87721df..dd2df157 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -43,7 +43,7 @@ export abstract class Panel { return this.build() } - private __responed__(viewId: string, action: string, args: any) { + private __responedCallback__(viewId: string, callbackId: string) { } } \ No newline at end of file diff --git a/js-framework/src/ui/view.ts b/js-framework/src/ui/view.ts index aa1d59d8..be525729 100644 --- a/js-framework/src/ui/view.ts +++ b/js-framework/src/ui/view.ts @@ -2,6 +2,7 @@ import { Color, GradientColor } from "../util/color" import { Modeling, Model } from "../util/types"; import "reflect-metadata" import { uniqueId } from "../util/uniqueId"; +import { loge } from "../util/log"; export function Property(target: Object, propKey: string) { Reflect.defineMetadata(propKey, true, target) @@ -41,6 +42,19 @@ export abstract class View implements Modeling { @Property viewId = uniqueId('ViewId') + callbacks: Map = new Map + + private callback2Id(f: Function) { + const id = uniqueId('Function') + this.callbacks.set(id, f) + return id + } + + private id2Callback(id: string) { + const f = this.callbacks.get(id) + return f + } + constructor() { return new Proxy(this, { get: (target, p) => { @@ -91,7 +105,9 @@ export abstract class View implements Modeling { __dirty_props__: { [index: string]: Model | undefined } = {} onPropertyChanged(propKey: string, oldV: Model, newV: Model): void { - if (newV instanceof Object + if (newV instanceof Function) { + newV = this.callback2Id(newV) + } else if (newV instanceof Object && Reflect.has(newV, 'toModel') && Reflect.get(newV, 'toModel') instanceof Function) { newV = Reflect.apply(Reflect.get(newV, 'toModel'), newV, []) @@ -99,6 +115,18 @@ export abstract class View implements Modeling { this.__dirty_props__[propKey] = newV } + responseCallback(id: string) { + const f = this.id2Callback(id) + if (f instanceof Function) { + const argumentsList: any = [] + for (let i = 1; i < arguments.length; i++) { + argumentsList.push(arguments[i]) + } + Reflect.apply(f, this, argumentsList) + } else { + loge(`Cannot find callback:${id} for ${JSON.stringify(this.toModel())}`) + } + } toModel() { return { id: this.viewId, diff --git a/js-framework/src/util/log.ts b/js-framework/src/util/log.ts new file mode 100644 index 00000000..8dce4118 --- /dev/null +++ b/js-framework/src/util/log.ts @@ -0,0 +1,11 @@ +export function log(message: any) { + console.log(message) +} + +export function loge(message: any) { + console.error(message) +} + +export function logw(message: any) { + console.warn(message) +}