add doric driver and context and soon

This commit is contained in:
pengfei.zhou 2019-12-19 11:52:15 +08:00
parent 25a6f08a96
commit 48d64fdf1e
5 changed files with 122 additions and 15 deletions

16
src/DoricContext.ts Normal file
View File

@ -0,0 +1,16 @@
import { DoricPlugin } from "./DoricPlugin"
const doricContexts: Map<string, DoricContext> = new Map
export function getDoricContext(contextId: string) {
return doricContexts.get(contextId)
}
export class DoricContext {
contextId: string
pluginInstances: Map<string, DoricPlugin> = new Map
constructor(contextId: string) {
this.contextId = contextId
doricContexts.set(contextId, this)
}
}

74
src/DoricDriver.ts Normal file
View File

@ -0,0 +1,74 @@
import { jsObtainContext } from 'doric/src/runtime/sandbox'
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
let scriptId = 0
function getScriptId() {
return `script_${scriptId++}`
}
let contexId = 0
export function getContextId() {
return `context_${contexId++}`
}
export function injectGlobalObject(name: string, value: any) {
Reflect.set(window, name, value, window)
}
export function loadJS(script: string) {
const scriptElement = document.createElement('script')
scriptElement.text = script
scriptElement.id = getScriptId()
document.body.appendChild(scriptElement)
}
function packageModuleScript(name: string, content: string) {
return `Reflect.apply(doric.jsRegisterModule,this,[${name},Reflect.apply(function(__module){(function(module,exports,require){
${content}
})(__module,__module.exports,doric.__require__);
return __module.exports;},this,[{exports:{}}])])`
}
function packageCreateContext(contextId: string, content: string) {
return `Reflect.apply(function(doric,context,Entry,require,exports){
${content}
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}])`
}
function initDoric() {
injectGlobalObject('nativeLog', (type: 'd' | 'w' | 'e', message: string) => {
switch (type) {
case 'd':
console.log(message)
break
case 'w':
console.warn(message)
break
case 'e':
console.error(message)
break
}
})
injectGlobalObject('nativeRequire', (moduleName: string) => {
const bundle = acquireJSBundle(moduleName)
if (bundle === undefined || bundle.length === 0) {
console.log(`Cannot require JS Bundle :${moduleName}`)
return false
} else {
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle)))
return true
}
})
injectGlobalObject('nativeBridge', (contextId: string, namespace: string, method: string, callbackId?: string, args?: any) => {
const context = jsObtainContext(contextId)
const pluginClass = acquirePlugin(namespace)
return true
})
}
initDoric()

View File

@ -1,24 +1,10 @@
import axios from 'axios'
import { jsCallReject } from 'doric/src/runtime/sandbox'
import { getContextId } from './DoricDriver'
let contexId = 0
function getContextId() {
return `${contexId++}`
}
function initDoric() {
jsCallReject('', '')
}
initDoric()
export class DoricElement extends HTMLElement {
source: string
alias: string
constructor() {
super()
this.source = this.getAttribute('src') || ""

9
src/DoricPlugin.ts Normal file
View File

@ -0,0 +1,9 @@
import { DoricContext } from "./DoricContext"
export type DoricPluginClass = { new(...args: any[]): {} }
export class DoricPlugin {
context: DoricContext
constructor(context: DoricContext) {
this.context = context
}
}

22
src/DoricRegistry.ts Normal file
View File

@ -0,0 +1,22 @@
import { DoricPluginClass } from "./DoricPlugin"
const bundles: Map<string, string> = new Map
const plugins: Map<string, DoricPluginClass> = new Map
export function acquireJSBundle(name: string) {
return bundles.get(name)
}
export function registerJSBundle(name: string, bundle: string) {
bundles.set(name, bundle)
}
export function registerPlugin(name: string, plugin: DoricPluginClass) {
plugins.set(name, plugin)
}
export function acquirePlugin(name: string) {
return plugins.get(name)
}