test context entity method success

This commit is contained in:
pengfei.zhou 2019-12-19 13:07:33 +08:00
parent c2b8abeadb
commit 1b65fd5ba0
7 changed files with 165 additions and 31 deletions

4
dist/Counter.js vendored
View File

@ -12,6 +12,10 @@ var __metadata = (undefined && undefined.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
class CounterView extends doric.ViewHolder {
test() {
console.log('Invoke Success')
}
build(root) {
root.addChild(doric.vlayout([
doric.text({

138
dist/index.js vendored
View File

@ -1478,7 +1478,7 @@ var doric = (function (exports) {
/**--------SandBox--------*/
/**++++++++Lib++++++++*/
Reflect.apply(doric.jsRegisterModule,this,[doric,Reflect.apply(function(__module){(function(module,exports,require){
Reflect.apply(doric.jsRegisterModule,this,["doric",Reflect.apply(function(__module){(function(module,exports,require){
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
@ -3537,14 +3537,134 @@ return __module.exports;
axios = axios && axios.hasOwnProperty('default') ? axios['default'] : axios;
let contexId = 0;
function getContextId() {
return `${contexId++}`;
const bundles = new Map;
const plugins = new Map;
function acquireJSBundle(name) {
return bundles.get(name);
}
function acquirePlugin(name) {
return plugins.get(name);
}
let __scriptId__ = 0;
function getScriptId() {
return `script_${__scriptId__++}`;
}
function injectGlobalObject(name, value) {
Reflect.set(window, name, value, window);
}
function loadJS(script) {
const scriptElement = document.createElement('script');
scriptElement.text = script;
scriptElement.id = getScriptId();
document.body.appendChild(scriptElement);
}
function packageModuleScript(name, content) {
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, content) {
return `Reflect.apply(function(doric,context,Entry,require,exports){
${content}
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}])`;
}
function initDoric() {
sandbox.jsCallReject('', '');
injectGlobalObject('nativeLog', (type, message) => {
switch (type) {
case 'd':
console.log(message);
break;
case 'w':
console.warn(message);
break;
case 'e':
console.error(message);
break;
}
});
injectGlobalObject('nativeRequire', (moduleName) => {
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, namespace, method, callbackId, args) => {
const pluginClass = acquirePlugin(namespace);
const doricContext = getDoricContext(contextId);
if (pluginClass === undefined) {
console.error(`Cannot find Plugin:${namespace}`);
return false;
}
if (doricContext === undefined) {
console.error(`Cannot find Doric Context:${contextId}`);
return false;
}
let plugin = doricContext.pluginInstances.get(namespace);
if (plugin === undefined) {
plugin = new pluginClass(doricContext);
doricContext.pluginInstances.set(namespace, plugin);
}
if (!Reflect.has(plugin, method)) {
console.error(`Cannot find Method:${method} in plugin ${namespace}`);
return false;
}
const pluginMethod = Reflect.get(plugin, method, plugin);
if (typeof pluginMethod !== 'function') {
console.error(`Plugin ${namespace}'s property ${method}'s type is ${typeof pluginMethod} not function,`);
}
const ret = Reflect.apply(pluginMethod, plugin, [args]);
if (ret instanceof Promise) {
ret.then(e => {
sandbox.jsCallResolve(contextId, callbackId, e);
}, e => {
sandbox.jsCallReject(contextId, callbackId, e);
});
}
else {
sandbox.jsCallResolve(contextId, callbackId, ret);
}
return true;
});
}
function createContext(contextId, content) {
loadJS(packageCreateContext(contextId, content));
}
initDoric();
const doricContexts = new Map;
let __contextId__ = 0;
function getContextId() {
return `context_${__contextId__++}`;
}
function getDoricContext(contextId) {
return doricContexts.get(contextId);
}
class DoricContext {
constructor(content) {
this.contextId = getContextId();
this.pluginInstances = new Map;
createContext(this.contextId, content);
doricContexts.set(this.contextId, this);
}
get context() {
return sandbox.jsObtainContext(this.contextId);
}
get panel() {
var _a;
return (_a = this.context) === null || _a === void 0 ? void 0 : _a.entity;
}
getEntityMethod(method) {
return Reflect.get(this.panel, method, this.panel);
}
}
class DoricElement extends HTMLElement {
constructor() {
super();
@ -3555,12 +3675,8 @@ return __module.exports;
});
}
load(content) {
const script = document.createElement('script');
const contextId = getContextId();
script.text = `Reflect.apply(function(doric,context,Entry,require,exports){
${content}
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}]);`;
this.append(script);
this.context = new DoricContext(content);
this.context.getEntityMethod('log')();
}
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -14,7 +14,7 @@ ${sandboxBundle}
/**--------SandBox--------*/
/**++++++++Lib++++++++*/
Reflect.apply(doric.jsRegisterModule,this,[doric,Reflect.apply(function(__module){(function(module,exports,require){
Reflect.apply(doric.jsRegisterModule,this,["doric",Reflect.apply(function(__module){(function(module,exports,require){
${doricLibBundle}
})(__module,__module.exports,doric.__require__);
return __module.exports;

View File

@ -1,16 +1,35 @@
import { jsObtainContext } from 'doric/src/runtime/sandbox'
import { Panel } from 'doric'
import { DoricPlugin } from "./DoricPlugin"
import { createContext } from "./DoricDriver"
const doricContexts: Map<string, DoricContext> = new Map
let __contextId__ = 0
function getContextId() {
return `context_${__contextId__++}`
}
export function getDoricContext(contextId: string) {
return doricContexts.get(contextId)
}
export class DoricContext {
contextId: string
contextId = getContextId()
pluginInstances: Map<string, DoricPlugin> = new Map
constructor(contextId: string) {
this.contextId = contextId
doricContexts.set(contextId, this)
constructor(content: string) {
createContext(this.contextId, content)
doricContexts.set(this.contextId, this)
}
get context() {
return jsObtainContext(this.contextId)
}
get panel() {
return this.context?.entity as Panel
}
getEntityMethod(method: string) {
return Reflect.get(this.panel, method, this.panel) as Function
}
}

View File

@ -1,4 +1,4 @@
import { jsObtainContext, jsCallResolve, jsCallReject } from 'doric/src/runtime/sandbox'
import { jsCallResolve, jsCallReject } from 'doric/src/runtime/sandbox'
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
import { getDoricContext } from './DoricContext'
import { DoricPlugin } from './DoricPlugin'
@ -9,10 +9,6 @@ function getScriptId() {
}
let __contextId__ = 0
export function getContextId() {
return `context_${__contextId__++}`
}
export function injectGlobalObject(name: string, value: any) {
Reflect.set(window, name, value, window)
@ -38,7 +34,6 @@ ${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) {
@ -104,5 +99,8 @@ function initDoric() {
})
}
export function createContext(contextId: string, content: string) {
loadJS(packageCreateContext(contextId, content))
}
initDoric()

View File

@ -1,10 +1,11 @@
import axios from 'axios'
import { getContextId } from './DoricDriver'
import { DoricContext } from './DoricContext'
export class DoricElement extends HTMLElement {
source: string
alias: string
context?: DoricContext
constructor() {
super()
this.source = this.getAttribute('src') || ""
@ -15,11 +16,7 @@ export class DoricElement extends HTMLElement {
}
load(content: string) {
const script = document.createElement('script');
const contextId = getContextId();
script.text = `Reflect.apply(function(doric,context,Entry,require,exports){
${content}
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}]);`
this.append(script)
this.context = new DoricContext(content)
this.context.getEntityMethod('log')()
}
}