native<=>js bridge
This commit is contained in:
parent
771cb338b6
commit
25938fb3cd
@ -1,6 +1,6 @@
|
||||
/**
|
||||
* ``` TypeScript
|
||||
* // run in global scope
|
||||
* // load script in global scope
|
||||
* Reflect.apply(
|
||||
* function(hego,context,require){
|
||||
* //Script content
|
||||
@ -10,7 +10,7 @@
|
||||
* hego.obtainContext(id),
|
||||
* hego.__require__,
|
||||
* ])
|
||||
*
|
||||
* // load module in global scope
|
||||
* Reflect.apply(hego.registerModule,this,[
|
||||
* moduleName,
|
||||
* Reflect.apply(function(__module){
|
||||
@ -21,31 +21,101 @@
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
declare function nativeRequire(moduleName: string): boolean
|
||||
|
||||
export interface Context {
|
||||
id: number
|
||||
asyncCall(module: string, method: string, args: any, callbackId: string): any
|
||||
declare function nativeBridge(contextId: string, namespace: string, method: string, args?: any, callbackId?: string): boolean
|
||||
|
||||
let __uniqueId__: number = 0
|
||||
|
||||
function uniqueId(prefix: string) {
|
||||
return `__${prefix}_${__uniqueId__++}__`;
|
||||
}
|
||||
|
||||
const gContexts = new Map
|
||||
const gModules = new Map
|
||||
export function log(message: any) {
|
||||
console.log(message)
|
||||
}
|
||||
|
||||
export function obtainContext(id: string) {
|
||||
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) {
|
||||
loge(`Cannot find context for context id:${contextId}`)
|
||||
return
|
||||
}
|
||||
const callback = context.callbacks.get(callbackId)
|
||||
if (callback === undefined) {
|
||||
loge(`Cannot find call for context id:${contextId},callback id:${callbackId}`)
|
||||
return
|
||||
}
|
||||
const argumentsList: any = []
|
||||
for (let i = 2; i < arguments.length; i++) {
|
||||
argumentsList.push(arguments[i])
|
||||
}
|
||||
Reflect.apply(callback.resolve, context, args)
|
||||
}
|
||||
|
||||
export function jsCallReject(contextId: string, callbackId: string, args?: any) {
|
||||
const context = gContexts.get(contextId)
|
||||
if (context === undefined) {
|
||||
loge(`Cannot find context for context id:${contextId}`)
|
||||
return
|
||||
}
|
||||
const callback = context.callbacks.get(callbackId)
|
||||
if (callback === undefined) {
|
||||
loge(`Cannot find call for context id:${contextId},callback id:${callbackId}`)
|
||||
return
|
||||
}
|
||||
const argumentsList: any = []
|
||||
for (let i = 2; i < arguments.length; i++) {
|
||||
argumentsList.push(arguments[i])
|
||||
}
|
||||
Reflect.apply(callback.reject, context, args)
|
||||
}
|
||||
|
||||
export class Context {
|
||||
id: string
|
||||
callbacks: Map<string, { resolve: Function, reject: Function }> = new Map
|
||||
constructor(id: string) {
|
||||
this.id = id
|
||||
}
|
||||
callNative(namespace: string, method: string, args?: any): Promise<any> {
|
||||
const callbackId = uniqueId('callback')
|
||||
|
||||
nativeBridge(this.id, namespace, method, args, callbackId)
|
||||
return new Promise((resolve, reject) => {
|
||||
this.callbacks.set(callbackId, {
|
||||
resolve,
|
||||
reject,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const gContexts: Map<string, Context> = new Map
|
||||
const gModules: Map<string, any> = new Map
|
||||
|
||||
export function jsObtainContext(id: string) {
|
||||
if (gContexts.has(id)) {
|
||||
return gContexts.get(id)
|
||||
} else {
|
||||
const context = { id }
|
||||
const context: Context = new Context(id)
|
||||
gContexts.set(id, context)
|
||||
return context
|
||||
}
|
||||
}
|
||||
|
||||
export function releaseContext(id: string) {
|
||||
export function jsReleaseContext(id: string) {
|
||||
gContexts.delete(id)
|
||||
}
|
||||
|
||||
declare function nativeRequire(name: string): boolean
|
||||
|
||||
export function __require__(name: string): any {
|
||||
if (gModules.has(name)) {
|
||||
return gModules.get(name)
|
||||
@ -57,6 +127,12 @@ export function __require__(name: string): any {
|
||||
}
|
||||
}
|
||||
}
|
||||
export function registerModule(name: string, moduleObject: any) {
|
||||
|
||||
export function jsRegisterModule(name: string, moduleObject: any) {
|
||||
gModules.set(name, moduleObject)
|
||||
}
|
||||
|
||||
export function jsCall() {
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user