js:compat es5,use context.sendNative to replace Proxy
This commit is contained in:
@@ -24,35 +24,32 @@ export function animate(context: BridgeContext) {
|
||||
const entity = context.entity
|
||||
if (entity instanceof Panel) {
|
||||
let panel = entity
|
||||
return (args: {
|
||||
return async (args: {
|
||||
animations: () => void,
|
||||
duration: number,
|
||||
}) => {
|
||||
return takeLet(panel.context.animate)(it => {
|
||||
return it.submit().then(() => {
|
||||
args.animations()
|
||||
return takeLet(panel.getRootView())(root => {
|
||||
if (root.isDirty()) {
|
||||
const model = root.toModel();
|
||||
(model as any).duration = args.duration
|
||||
const ret = it.animateRender(model)
|
||||
root.clean()
|
||||
return ret
|
||||
await context.callNative('animate', 'submit')
|
||||
args.animations()
|
||||
return takeLet(panel.getRootView())(root => {
|
||||
if (root.isDirty()) {
|
||||
const model = root.toModel();
|
||||
(model as any).duration = args.duration
|
||||
const ret = context.callNative('animate', 'animateRender', model)
|
||||
root.clean()
|
||||
return ret
|
||||
}
|
||||
for (let map of panel.allHeadViews()) {
|
||||
for (let v of map.values()) {
|
||||
if (v.isDirty()) {
|
||||
const model_1 = v.toModel()
|
||||
const ret_1 = context.callNative('animate', 'animateRender', model_1)
|
||||
v.clean()
|
||||
return ret_1
|
||||
}
|
||||
for (let map of panel.allHeadViews()) {
|
||||
for (let v of map.values()) {
|
||||
if (v.isDirty()) {
|
||||
const model = v.toModel()
|
||||
const ret = it.animateRender(model)
|
||||
it.clean()
|
||||
return ret
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error('Cannot find any animated elements')
|
||||
})
|
||||
})
|
||||
}) as Promise<any>
|
||||
}
|
||||
}
|
||||
throw new Error('Cannot find any animated elements')
|
||||
})
|
||||
}
|
||||
} else {
|
||||
return (args: {
|
||||
|
@@ -19,7 +19,7 @@ import { Gravity } from "../util/gravity"
|
||||
export function modal(context: BridgeContext) {
|
||||
return {
|
||||
toast: (msg: string, gravity: Gravity = Gravity.Bottom) => {
|
||||
context.modal.toast({
|
||||
context.callNative('modal', 'toast', {
|
||||
msg,
|
||||
gravity: gravity.toModel(),
|
||||
})
|
||||
@@ -30,9 +30,9 @@ export function modal(context: BridgeContext) {
|
||||
okLabel?: string,
|
||||
}) => {
|
||||
if (typeof arg === 'string') {
|
||||
return context.modal.alert({ msg: arg })
|
||||
return context.callNative('modal', 'alert', { msg: arg })
|
||||
} else {
|
||||
return context.modal.alert(arg)
|
||||
return context.callNative('modal', 'alert', arg)
|
||||
}
|
||||
},
|
||||
confirm: (arg: string | {
|
||||
@@ -42,9 +42,9 @@ export function modal(context: BridgeContext) {
|
||||
cancelLabel?: string,
|
||||
}) => {
|
||||
if (typeof arg === 'string') {
|
||||
return context.modal.confirm({ msg: arg })
|
||||
return context.callNative('modal', 'confirm', { msg: arg })
|
||||
} else {
|
||||
return context.modal.confirm(arg)
|
||||
return context.callNative('modal', 'confirm', arg)
|
||||
}
|
||||
},
|
||||
prompt: (arg: {
|
||||
@@ -55,7 +55,7 @@ export function modal(context: BridgeContext) {
|
||||
text?: string,
|
||||
defaultText?: string,
|
||||
}) => {
|
||||
return context.modal.prompt(arg) as Promise<string>
|
||||
return context.callNative('modal', 'prompt', arg) as Promise<string>
|
||||
},
|
||||
}
|
||||
}
|
@@ -27,36 +27,30 @@ export function navbar(context: BridgeContext) {
|
||||
|
||||
return {
|
||||
isHidden: () => {
|
||||
return context.navbar.isHidden() as Promise<boolean>
|
||||
return context.callNative('navbar', 'isHidden') as Promise<boolean>
|
||||
},
|
||||
setHidden: (hidden: boolean) => {
|
||||
return context.navbar.setHidden({
|
||||
hidden,
|
||||
})
|
||||
return context.callNative('navbar', 'setHidden', { hidden, })
|
||||
},
|
||||
setTitle: (title: string) => {
|
||||
return context.navbar.setTitle({
|
||||
title,
|
||||
})
|
||||
return context.callNative('navbar', 'setTitle', { title, })
|
||||
},
|
||||
setBgColor: (color: Color) => {
|
||||
return context.navbar.setBgColor({
|
||||
color: color.toModel(),
|
||||
})
|
||||
return context.callNative('navbar', 'setBgColor', { color: color.toModel(), })
|
||||
},
|
||||
setLeft: (view: View) => {
|
||||
if (panel) {
|
||||
panel.clearHeadViews("navbar_left")
|
||||
panel.addHeadView("navbar_left", view)
|
||||
}
|
||||
return context.navbar.setLeft(view.toModel())
|
||||
return context.callNative('navbar', 'setLeft', view.toModel())
|
||||
},
|
||||
setRight: (view: View) => {
|
||||
if (panel) {
|
||||
panel.clearHeadViews("navbar_right")
|
||||
panel.addHeadView("navbar_right", view)
|
||||
}
|
||||
return context.navbar.setRight(view.toModel())
|
||||
return context.callNative('navbar', 'setRight', view.toModel())
|
||||
}
|
||||
}
|
||||
}
|
@@ -25,12 +25,12 @@ export function navigator(context: BridgeContext) {
|
||||
if (config && config.extra) {
|
||||
(config as any).extra = JSON.stringify(config.extra)
|
||||
}
|
||||
return context.navigator.push({
|
||||
return context.callNative('navigator', 'push', {
|
||||
scheme, config
|
||||
})
|
||||
},
|
||||
pop: (animated = true) => {
|
||||
return context.navigator.pop({ animated })
|
||||
return context.callNative('navigator', 'pop', { animated })
|
||||
},
|
||||
}
|
||||
}
|
@@ -61,7 +61,7 @@ function transformRequest(request: IRequest) {
|
||||
export function network(context: BridgeContext) {
|
||||
return {
|
||||
request: (config: IRequest) => {
|
||||
return context.network.request(transformRequest(config)) as Promise<IResponse>
|
||||
return context.callNative('network', 'request', transformRequest(config)) as Promise<IResponse>
|
||||
},
|
||||
get: (url: string, config?: IRequest) => {
|
||||
let finalConfig = config
|
||||
@@ -70,7 +70,7 @@ export function network(context: BridgeContext) {
|
||||
}
|
||||
finalConfig.url = url
|
||||
finalConfig.method = "get"
|
||||
return context.network.request(transformRequest(finalConfig)) as Promise<IResponse>
|
||||
return context.callNative('network', 'request', transformRequest(finalConfig)) as Promise<IResponse>
|
||||
},
|
||||
post: (url: string, data?: object | string, config?: IRequest) => {
|
||||
let finalConfig = config
|
||||
@@ -82,7 +82,7 @@ export function network(context: BridgeContext) {
|
||||
if (data !== undefined) {
|
||||
finalConfig.data = data
|
||||
}
|
||||
return context.network.request(transformRequest(finalConfig)) as Promise<IResponse>
|
||||
return context.callNative('network', 'request', transformRequest(finalConfig)) as Promise<IResponse>
|
||||
},
|
||||
put: (url: string, data?: object | string, config?: IRequest) => {
|
||||
let finalConfig = config
|
||||
@@ -94,7 +94,7 @@ export function network(context: BridgeContext) {
|
||||
if (data !== undefined) {
|
||||
finalConfig.data = data
|
||||
}
|
||||
return context.network.request(transformRequest(finalConfig)) as Promise<IResponse>
|
||||
return context.callNative('network', 'request', transformRequest(finalConfig)) as Promise<IResponse>
|
||||
},
|
||||
delete: (url: string, data?: object | string, config?: IRequest) => {
|
||||
let finalConfig = config
|
||||
@@ -103,7 +103,7 @@ export function network(context: BridgeContext) {
|
||||
}
|
||||
finalConfig.url = url
|
||||
finalConfig.method = "delete"
|
||||
return context.network.request(transformRequest(finalConfig)) as Promise<IResponse>
|
||||
return context.callNative('network', 'request', transformRequest(finalConfig)) as Promise<IResponse>
|
||||
},
|
||||
}
|
||||
}
|
@@ -20,15 +20,15 @@ export function notification(context: BridgeContext) {
|
||||
if (args.data !== undefined) {
|
||||
(args as any).data = JSON.stringify(args.data)
|
||||
}
|
||||
return context.notification.publish(args)
|
||||
return context.callNative('notification', 'publish', args)
|
||||
},
|
||||
subscribe: (args: { biz?: string, name: string, callback: (data?: any) => void, androidSystem?: boolean }) => {
|
||||
(args as any).callback = (context as any).function2Id(args.callback)
|
||||
return context.notification.subscribe(args) as Promise<string>
|
||||
(args as any).callback = context.function2Id(args.callback)
|
||||
return context.callNative('notification', 'subscribe', args) as Promise<string>
|
||||
},
|
||||
unsubscribe: (subscribeId: string) => {
|
||||
(context as any).removeFuncById(subscribeId)
|
||||
return context.notification.unsubscribe(subscribeId)
|
||||
context.removeFuncById(subscribeId)
|
||||
return context.callNative('notification', 'unsubscribe', subscribeId)
|
||||
}
|
||||
}
|
||||
}
|
@@ -28,7 +28,7 @@ export function popover(context: BridgeContext) {
|
||||
if (panel) {
|
||||
panel.addHeadView("popover", view)
|
||||
}
|
||||
return context.popover.show(view.toModel())
|
||||
return context.callNative('popover', 'show', view.toModel())
|
||||
},
|
||||
dismiss: (view: View | undefined = undefined) => {
|
||||
if (panel) {
|
||||
@@ -38,7 +38,7 @@ export function popover(context: BridgeContext) {
|
||||
panel.clearHeadViews("popover")
|
||||
}
|
||||
}
|
||||
return context.popover.dismiss(view ? { id: view.viewId } : undefined)
|
||||
return context.callNative('popover', 'dismiss', view ? { id: view.viewId } : undefined)
|
||||
},
|
||||
}
|
||||
}
|
@@ -21,13 +21,13 @@ export enum StatusBarMode { LIGHT, DARK }
|
||||
export function statusbar(context: BridgeContext) {
|
||||
return {
|
||||
setHidden: (hidden: boolean) => {
|
||||
return context.statusbar.setHidden({hidden})
|
||||
return context.callNative('statusbar', 'setHidden', { hidden })
|
||||
},
|
||||
setMode: (mode: StatusBarMode) => {
|
||||
return context.statusbar.setMode({mode})
|
||||
return context.callNative('statusbar', 'setMode', { mode })
|
||||
},
|
||||
setColor: (color: Color) => {
|
||||
return context.statusbar.setColor({color: color.toModel()})
|
||||
return context.callNative('statusbar', 'setColor', { color: color.toModel() })
|
||||
},
|
||||
}
|
||||
}
|
@@ -18,16 +18,16 @@ import { BridgeContext } from "../runtime/global"
|
||||
export function storage(context: BridgeContext) {
|
||||
return {
|
||||
setItem: (key: string, value: string, zone?: string) => {
|
||||
return context.storage.setItem({ key, value, zone })
|
||||
return context.callNative('storage', 'setItem', { key, value, zone })
|
||||
},
|
||||
getItem: (key: string, zone?: string) => {
|
||||
return context.storage.getItem({ key, zone }) as Promise<string>
|
||||
return context.callNative('storage', 'getItem', { key, zone }) as Promise<string>
|
||||
},
|
||||
remove: (key: string, zone?: string) => {
|
||||
return context.storage.remove({ key, zone })
|
||||
return context.callNative('storage', 'remove', { key, zone })
|
||||
},
|
||||
clear: (zone: string) => {
|
||||
return context.storage.clear({ zone })
|
||||
return context.callNative('storage', 'clear', { zone })
|
||||
},
|
||||
}
|
||||
}
|
@@ -16,7 +16,31 @@
|
||||
export * from 'reflect-metadata'
|
||||
|
||||
export type BridgeContext = {
|
||||
[index: string]: { [index: string]: (args?: any) => Promise<any> }
|
||||
/**
|
||||
* The identify of current context
|
||||
*/
|
||||
id: string
|
||||
/**
|
||||
* In this case,It's current panel
|
||||
*/
|
||||
entity: any
|
||||
/**
|
||||
* call native plugin
|
||||
* @param namespace
|
||||
* @param method
|
||||
* @param args
|
||||
*/
|
||||
callNative(namespace: string, method: string, args?: any): Promise<any>
|
||||
/**
|
||||
* Transform function to functionId as string
|
||||
* @param func
|
||||
*/
|
||||
function2Id(func: Function): string
|
||||
/**
|
||||
* Remove transformed functions
|
||||
* @param funcId
|
||||
*/
|
||||
removeFuncById(funcId: string): void
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
@@ -157,7 +157,7 @@ export abstract class Panel {
|
||||
}
|
||||
|
||||
private nativeRender(model: Model) {
|
||||
(this.context as any).callNative("shader", "render", model)
|
||||
this.context.callNative("shader", "render", model)
|
||||
}
|
||||
|
||||
private hookBeforeNativeCall() {
|
||||
|
@@ -122,7 +122,6 @@ export abstract class View implements Modeling, IView {
|
||||
@Property
|
||||
hidden?: boolean
|
||||
|
||||
@Property
|
||||
viewId = uniqueId('ViewId')
|
||||
|
||||
@Property
|
||||
@@ -152,6 +151,9 @@ export abstract class View implements Modeling, IView {
|
||||
}
|
||||
|
||||
private id2Callback(id: string) {
|
||||
if (this.callbacks === undefined) {
|
||||
this.callbacks = new Map
|
||||
}
|
||||
let f = this.callbacks.get(id)
|
||||
if (f === undefined) {
|
||||
f = Reflect.get(this, id) as Function
|
||||
@@ -280,10 +282,9 @@ export abstract class View implements Modeling, IView {
|
||||
return this
|
||||
}
|
||||
|
||||
nativeChannel(context: any, name: string) {
|
||||
nativeChannel(context: BridgeContext, name: string) {
|
||||
let thisView: View | undefined = this
|
||||
return function (args: any = undefined) {
|
||||
const func = context.shader.command
|
||||
const viewIds = []
|
||||
while (thisView != undefined) {
|
||||
viewIds.push(thisView.viewId)
|
||||
@@ -294,7 +295,8 @@ export abstract class View implements Modeling, IView {
|
||||
name,
|
||||
args,
|
||||
}
|
||||
return Reflect.apply(func, undefined, [params]) as Promise<any>
|
||||
|
||||
return context.callNative('shader', 'command', params) as Promise<any>
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -109,7 +109,6 @@ export abstract class View implements Modeling, IView {
|
||||
@Property
|
||||
hidden?: boolean
|
||||
|
||||
@Property
|
||||
viewId = uniqueId('ViewId')
|
||||
|
||||
@Property
|
||||
@@ -278,10 +277,9 @@ export abstract class View implements Modeling, IView {
|
||||
return this
|
||||
}
|
||||
|
||||
nativeChannel(context: any, name: string) {
|
||||
nativeChannel(context: BridgeContext, name: string) {
|
||||
let thisView: View | undefined = this
|
||||
return function (args: any = undefined) {
|
||||
const func = context.shader.command
|
||||
const viewIds = []
|
||||
while (thisView != undefined) {
|
||||
viewIds.push(thisView.viewId)
|
||||
@@ -292,7 +290,8 @@ export abstract class View implements Modeling, IView {
|
||||
name,
|
||||
args,
|
||||
}
|
||||
return Reflect.apply(func, undefined, [params]) as Promise<any>
|
||||
|
||||
return context.callNative('shader', 'command', params) as Promise<any>
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user