weex-vue-framework
Advanced tools
Comparing version 2.2.1-weex.1 to 2.2.2-weex.1
238
index.js
@@ -37,3 +37,3 @@ 'use strict'; | ||
renderer.Comment = cfg.Comment; | ||
renderer.sendTasks = cfg.sendTasks; | ||
renderer.compileBundle = cfg.compileBundle; | ||
} | ||
@@ -51,3 +51,3 @@ | ||
delete renderer.Comment; | ||
delete renderer.sendTasks; | ||
delete renderer.compileBundle; | ||
} | ||
@@ -87,14 +87,5 @@ | ||
// All function/callback of parameters before sent to native | ||
// will be converted as an id. So `callbacks` is used to store | ||
// these real functions. When a callback invoked and won't be | ||
// called again, it should be removed from here automatically. | ||
var callbacks = []; | ||
// The latest callback id, incremental. | ||
var callbackId = 1; | ||
var instance = instances[instanceId] = { | ||
instanceId: instanceId, config: config, data: data, | ||
document: document, callbacks: callbacks, callbackId: callbackId | ||
document: document | ||
}; | ||
@@ -114,3 +105,3 @@ | ||
// Each instance has a independent `Vue` mdoule instance | ||
// Each instance has a independent `Vue` module instance | ||
var Vue = instance.Vue = createVueModuleInstance(instanceId, moduleGetter); | ||
@@ -126,6 +117,11 @@ | ||
}, timerAPIs); | ||
callFunction(instanceVars, appCode); | ||
if (!callFunctionNative(instanceVars, appCode)) { | ||
// If failed to compile functionBody on native side, | ||
// fallback to 'callFunction()'. | ||
callFunction(instanceVars, appCode); | ||
} | ||
// Send `createFinish` signal to native. | ||
renderer.sendTasks(instanceId + '', [{ module: 'dom', method: 'createFinish', args: [] }], -1); | ||
instance.document.taskCenter.send('dom', { action: 'createFinish' }, []); | ||
} | ||
@@ -162,3 +158,3 @@ | ||
// Finally `refreshFinish` signal needed. | ||
renderer.sendTasks(instanceId + '', [{ module: 'dom', method: 'refreshFinish', args: [] }], -1); | ||
instance.document.taskCenter.send('dom', { action: 'refreshFinish' }, []); | ||
} | ||
@@ -178,45 +174,53 @@ | ||
var jsHandlers = { | ||
fireEvent: function (id) { | ||
var args = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ]; | ||
return fireEvent.apply(void 0, [ instances[id] ].concat( args )) | ||
}, | ||
callback: function (id) { | ||
var args = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ]; | ||
return callback.apply(void 0, [ instances[id] ].concat( args )) | ||
} | ||
}; | ||
function fireEvent (instance, nodeId, type, e, domChanges) { | ||
var el = instance.document.getRef(nodeId); | ||
if (el) { | ||
return instance.document.fireEvent(el, type, e, domChanges) | ||
} | ||
return new Error(("invalid element reference \"" + nodeId + "\"")) | ||
} | ||
function callback (instance, callbackId, data, ifKeepAlive) { | ||
var result = instance.document.taskCenter.callback(callbackId, data, ifKeepAlive); | ||
instance.document.taskCenter.send('dom', { action: 'updateFinish' }, []); | ||
return result | ||
} | ||
/** | ||
* Receive tasks from native. Generally there are two types of tasks: | ||
* 1. `fireEvent`: an device actions or user actions from native. | ||
* 2. `callback`: invoke function which sent to native as a parameter before. | ||
* @param {string} instanceId | ||
* @param {array} tasks | ||
* Accept calls from native (event or callback). | ||
* | ||
* @param {string} id | ||
* @param {array} tasks list with `method` and `args` | ||
*/ | ||
function receiveTasks (instanceId, tasks) { | ||
var instance = instances[instanceId]; | ||
if (!instance || !(instance.app instanceof instance.Vue)) { | ||
return new Error(("receiveTasks: instance " + instanceId + " not found!")) | ||
function receiveTasks (id, tasks) { | ||
var instance = instances[id]; | ||
if (instance && Array.isArray(tasks)) { | ||
var results = []; | ||
tasks.forEach(function (task) { | ||
var handler = jsHandlers[task.method]; | ||
var args = [].concat( task.args ); | ||
/* istanbul ignore else */ | ||
if (typeof handler === 'function') { | ||
args.unshift(id); | ||
results.push(handler.apply(void 0, args)); | ||
} | ||
}); | ||
return results | ||
} | ||
var callbacks = instance.callbacks; | ||
var document = instance.document; | ||
tasks.forEach(function (task) { | ||
// `fireEvent` case: find the event target and fire. | ||
if (task.method === 'fireEvent') { | ||
var ref = task.args; | ||
var nodeId = ref[0]; | ||
var type = ref[1]; | ||
var e = ref[2]; | ||
var domChanges = ref[3]; | ||
var el = document.getRef(nodeId); | ||
document.fireEvent(el, type, e, domChanges); | ||
} | ||
// `callback` case: find the callback by id and call it. | ||
if (task.method === 'callback') { | ||
var ref$1 = task.args; | ||
var callbackId = ref$1[0]; | ||
var data = ref$1[1]; | ||
var ifKeepAlive = ref$1[2]; | ||
var callback = callbacks[callbackId]; | ||
if (typeof callback === 'function') { | ||
callback(data); | ||
// Remove the callback from `callbacks` if it won't called again. | ||
if (typeof ifKeepAlive === 'undefined' || ifKeepAlive === false) { | ||
callbacks[callbackId] = undefined; | ||
} | ||
} | ||
} | ||
}); | ||
// Finally `updateFinish` signal needed. | ||
renderer.sendTasks(instanceId + '', [{ module: 'dom', method: 'updateFinish', args: [] }], -1); | ||
return new Error(("invalid instance id \"" + id + "\" or tasks")) | ||
} | ||
@@ -322,7 +326,5 @@ | ||
* Generate native module getter. Each native module has several | ||
* methods to call. And all the hebaviors is instance-related. So | ||
* methods to call. And all the behaviors is instance-related. So | ||
* this getter will return a set of methods which additionally | ||
* send current instance id to native when called. Also the args | ||
* will be normalized into "safe" value. For example function arg | ||
* will be converted into a callback id. | ||
* send current instance id to native when called. | ||
* @param {string} instanceId | ||
@@ -337,11 +339,19 @@ * @return {function} | ||
var loop = function ( methodName ) { | ||
output[methodName] = function () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
Object.defineProperty(output, methodName, { | ||
enumerable: true, | ||
configurable: true, | ||
get: function proxyGetter () { | ||
return function () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
var finalArgs = args.map(function (value) { | ||
return normalize(value, instance) | ||
}); | ||
renderer.sendTasks(instanceId + '', [{ module: name, method: methodName, args: finalArgs }], -1); | ||
}; | ||
return instance.document.taskCenter.send('module', { module: name, method: methodName }, args) | ||
} | ||
}, | ||
set: function proxySetter (val) { | ||
if (typeof val === 'function') { | ||
return instance.document.taskCenter.send('module', { module: name, method: methodName }, [val]) | ||
} | ||
} | ||
}); | ||
}; | ||
@@ -357,3 +367,3 @@ | ||
* will be converted into callback id when sent to native. So the | ||
* framework can make sure no side effect of the callabck happened after | ||
* framework can make sure no side effect of the callback happened after | ||
* an instance destroyed. | ||
@@ -365,3 +375,2 @@ * @param {[type]} instanceId [description] | ||
function getInstanceTimer (instanceId, moduleGetter) { | ||
var instance = instances[instanceId]; | ||
var timer = moduleGetter('timer'); | ||
@@ -377,3 +386,2 @@ var timerAPIs = { | ||
timer.setTimeout(handler, args[1]); | ||
return instance.callbackId.toString() | ||
}, | ||
@@ -388,3 +396,2 @@ setInterval: function () { | ||
timer.setInterval(handler, args[1]); | ||
return instance.callbackId.toString() | ||
}, | ||
@@ -421,48 +428,51 @@ clearTimeout: function (n) { | ||
/** | ||
* Convert all type of values into "safe" format to send to native. | ||
* 1. A `function` will be converted into callback id. | ||
* 2. An `Element` object will be converted into `ref`. | ||
* The `instance` param is used to generate callback id and store | ||
* function if necessary. | ||
* @param {any} v | ||
* @param {object} instance | ||
* @return {any} | ||
* Call a new function generated on the V8 native side. | ||
* | ||
* This function helps speed up bundle compiling. Normally, the V8 | ||
* engine needs to download, parse, and compile a bundle on every | ||
* visit. If 'compileBundle()' is available on native side, | ||
* the downloding, parsing, and compiling steps would be skipped. | ||
* @param {object} globalObjects | ||
* @param {string} body | ||
* @return {boolean} | ||
*/ | ||
function normalize (v, instance) { | ||
var type = typof(v); | ||
function callFunctionNative (globalObjects, body) { | ||
if (typeof renderer.compileBundle !== 'function') { | ||
return false | ||
} | ||
switch (type) { | ||
case 'undefined': | ||
case 'null': | ||
return '' | ||
case 'regexp': | ||
return v.toString() | ||
case 'date': | ||
return v.toISOString() | ||
case 'number': | ||
case 'string': | ||
case 'boolean': | ||
case 'array': | ||
case 'object': | ||
if (v instanceof renderer.Element) { | ||
return v.ref | ||
} | ||
return v | ||
case 'function': | ||
instance.callbacks[++instance.callbackId] = v; | ||
return instance.callbackId.toString() | ||
default: | ||
return JSON.stringify(v) | ||
var fn = void 0; | ||
var isNativeCompileOk = false; | ||
var script = '(function ('; | ||
var globalKeys = []; | ||
var globalValues = []; | ||
for (var key in globalObjects) { | ||
globalKeys.push(key); | ||
globalValues.push(globalObjects[key]); | ||
} | ||
} | ||
for (var i = 0; i < globalKeys.length - 1; ++i) { | ||
script += globalKeys[i]; | ||
script += ','; | ||
} | ||
script += globalKeys[globalKeys.length - 1]; | ||
script += ') {'; | ||
script += body; | ||
script += '} )'; | ||
/** | ||
* Get the exact type of an object by `toString()`. For example call | ||
* `toString()` on an array will be returned `[object Array]`. | ||
* @param {any} v | ||
* @return {string} | ||
*/ | ||
function typof (v) { | ||
var s = Object.prototype.toString.call(v); | ||
return s.substring(8, s.length - 1).toLowerCase() | ||
try { | ||
var weex = globalObjects.weex || {}; | ||
var config = weex.config || {}; | ||
fn = renderer.compileBundle(script, | ||
config.bundleUrl, | ||
config.bundleDigest, | ||
config.codeCachePath); | ||
if (fn && typeof fn === 'function') { | ||
fn.apply(void 0, globalValues); | ||
isNativeCompileOk = true; | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return isNativeCompileOk | ||
} | ||
@@ -469,0 +479,0 @@ |
{ | ||
"name": "weex-vue-framework", | ||
"version": "2.2.1-weex.1", | ||
"version": "2.2.2-weex.1", | ||
"description": "Vue 2.0 Framework for Weex", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166313
5733
1