add view response callback
This commit is contained in:
parent
e6bc920ab7
commit
b06e31fac6
@ -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) {
|
||||
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
@ -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<String, Function> = 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,
|
||||
|
11
js-framework/src/util/log.ts
Normal file
11
js-framework/src/util/log.ts
Normal file
@ -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)
|
||||
}
|
Reference in New Issue
Block a user