You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by da...@apache.org on 2017/08/17 03:59:42 UTC

[25/43] incubator-weex git commit: * [jsfm] refactor the logic in runtime init

* [jsfm] refactor the logic in runtime init


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/bade9551
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/bade9551
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/bade9551

Branch: refs/heads/0.16-dev
Commit: bade95514eb57eaf0dec7a4bf42f07b33f9c9aa6
Parents: 8294acf
Author: Hanks <zh...@gmail.com>
Authored: Wed Aug 9 00:26:17 2017 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Wed Aug 9 00:26:17 2017 +0800

----------------------------------------------------------------------
 html5/runtime/api/init.js          | 77 +++++++++++++++++++--------------
 html5/test/unit/default/runtime.js | 10 ++---
 html5/test/unit/vanilla/index.js   | 13 ------
 3 files changed, 49 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/bade9551/html5/runtime/api/init.js
----------------------------------------------------------------------
diff --git a/html5/runtime/api/init.js b/html5/runtime/api/init.js
index d9a877f..cd8af86 100644
--- a/html5/runtime/api/init.js
+++ b/html5/runtime/api/init.js
@@ -32,16 +32,18 @@ const versionRegExp = /^\s*\/\/ *(\{[^}]*\}) *\r?\n/
  * @param  {string} code
  * @return {object}
  */
-function checkVersion (code) {
-  let info
+function getBundleType (code) {
   const result = versionRegExp.exec(code)
   if (result) {
     try {
-      info = JSON.parse(result[1])
+      const info = JSON.parse(result[1])
+      return info.framework
     }
     catch (e) {}
   }
-  return info
+
+  // default bundle type
+  return 'Weex'
 }
 
 function createServices (id, env, config) {
@@ -66,6 +68,12 @@ function createServices (id, env, config) {
 
 const instanceMap = {}
 
+function getFrameworkType (id) {
+  if (instanceMap[id]) {
+    return instanceMap[id].framework
+  }
+}
+
 /**
  * Check which framework a certain JS Bundle code based to. And create instance
  * by this framework.
@@ -75,33 +83,35 @@ const instanceMap = {}
  * @param {object} data
  */
 function createInstance (id, code, config, data) {
-  let info = instanceMap[id]
+  if (instanceMap[id]) {
+    return new Error(`invalid instance id "${id}"`)
+  }
 
-  if (!info) {
-    // Init instance info.
-    info = checkVersion(code) || {}
-    if (!frameworks[info.framework]) {
-      info.framework = 'Weex'
-    }
+  // Init instance info.
+  const bundleType = getBundleType(code)
 
-    // Init instance config.
-    config = JSON.parse(JSON.stringify(config || {}))
-    config.bundleVersion = info.version
-    config.env = JSON.parse(JSON.stringify(global.WXEnvironment || {}))
-    console.debug(`[JS Framework] create an ${info.framework}@${config.bundleVersion} instance from ${config.bundleVersion}`)
-
-    const env = {
-      info,
-      config,
-      created: Date.now(),
-      framework: info.framework
-    }
-    env.services = createServices(id, env, runtimeConfig)
-    instanceMap[id] = env
+  // Init instance config.
+  config = JSON.parse(JSON.stringify(config || {}))
+  config.env = JSON.parse(JSON.stringify(global.WXEnvironment || {}))
+
+  const context = {
+    config,
+    created: Date.now(),
+    framework: bundleType
+  }
+  context.services = createServices(id, context, runtimeConfig)
+  instanceMap[id] = context
 
-    return frameworks[info.framework].createInstance(id, code, config, data, env)
+  if (process.env.NODE_ENV === 'development') {
+    console.debug(`[JS Framework] create an ${bundleType} instance`)
   }
-  return new Error(`invalid instance id "${id}"`)
+
+  const fm = frameworks[bundleType]
+  if (!fm) {
+    return new Error(`invalid bundle type "${bundleType}".`)
+  }
+
+  return fm.createInstance(id, code, config, data, context)
 }
 
 const methods = {
@@ -145,9 +155,10 @@ function checkComponentMethods (components) {
 function genInstance (methodName) {
   methods[methodName] = function (...args) {
     const id = args[0]
-    const info = instanceMap[id]
-    if (info && frameworks[info.framework]) {
-      const result = frameworks[info.framework][methodName](...args)
+    const type = getFrameworkType(id)
+    if (type && frameworks[type]) {
+      const result = frameworks[type][methodName](...args)
+      const info = { framework: type }
 
       // Lifecycle methods
       if (methodName === 'refreshInstance') {
@@ -183,9 +194,9 @@ function genInstance (methodName) {
 function adaptInstance (methodName, nativeMethodName) {
   methods[nativeMethodName] = function (...args) {
     const id = args[0]
-    const info = instanceMap[id]
-    if (info && frameworks[info.framework]) {
-      return frameworks[info.framework][methodName](...args)
+    const type = getFrameworkType(id)
+    if (type && frameworks[type]) {
+      return frameworks[type][methodName](...args)
     }
     return new Error(`invalid instance id "${id}"`)
   }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/bade9551/html5/test/unit/default/runtime.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/default/runtime.js b/html5/test/unit/default/runtime.js
index 1aa0a8c..9a52fff 100644
--- a/html5/test/unit/default/runtime.js
+++ b/html5/test/unit/default/runtime.js
@@ -193,7 +193,7 @@ describe('framework entry', () => {
       expect(frameworks.xxx.createInstance.firstCall.args.slice(0, 4)).eql([
         instanceId + '~',
         code,
-        { bundleVersion: '0.3.1', env: {}},
+        { env: {}},
         undefined
       ])
 
@@ -216,7 +216,7 @@ describe('framework entry', () => {
       expect(frameworks.Weex.createInstance.firstCall.args.slice(0, 4)).eql([
         instanceId + '~~~',
         code,
-        { bundleVersion: undefined, env: {}},
+        { env: {}},
         undefined
       ])
 
@@ -237,7 +237,7 @@ describe('framework entry', () => {
       expect(frameworks.yyy.createInstance.firstCall.args.slice(0, 4)).eql([
         instanceId + '~~~~',
         code,
-        { bundleVersion: undefined, env: {}},
+        { env: {}},
         undefined
       ])
 
@@ -250,7 +250,7 @@ describe('framework entry', () => {
       expect(frameworks.Weex.createInstance.secondCall.args.slice(0, 4)).eql([
         instanceId + '~~~~~',
         code,
-        { bundleVersion: undefined, env: {}},
+        { env: {}},
         undefined
       ])
 
@@ -263,7 +263,7 @@ describe('framework entry', () => {
       expect(frameworks.Weex.createInstance.thirdCall.args.slice(0, 4)).eql([
         instanceId + '~~~~~~',
         code,
-        { bundleVersion: undefined, env: {}},
+        { env: {}},
         undefined
       ])
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/bade9551/html5/test/unit/vanilla/index.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/vanilla/index.js b/html5/test/unit/vanilla/index.js
index f2bd338..6389c4c 100644
--- a/html5/test/unit/vanilla/index.js
+++ b/html5/test/unit/vanilla/index.js
@@ -32,25 +32,12 @@ function clearRefs (json) {
 describe('vanilla test', () => {
   it('standard APIs', () => {
     expect(vanilla.init).to.be.a('function')
-    expect(vanilla.registerComponents).to.be.a('function')
-    expect(vanilla.registerModules).to.be.a('function')
-    expect(vanilla.registerMethods).to.be.a('function')
-    expect(vanilla.prepareInstance).to.be.a('function')
     expect(vanilla.createInstance).to.be.a('function')
-    expect(vanilla.refreshInstance).to.be.a('function')
     expect(vanilla.destroyInstance).to.be.a('function')
     expect(vanilla.getRoot).to.be.a('function')
     expect(vanilla.receiveTasks).to.be.a('function')
   })
 
-  it('empty functions', () => {
-    expect(vanilla.registerComponents()).to.be.undefined
-    expect(vanilla.registerModules()).to.be.undefined
-    expect(vanilla.registerMethods()).to.be.undefined
-    expect(vanilla.prepareInstance()).to.be.undefined
-    expect(vanilla.refreshInstance()).to.be.undefined
-  })
-
   it('create & destroy instance', () => {
     const id = 'basic-demo'
     vanilla.init(runtime.config)