Github user jiyinyiyong commented on a diff in the pull request:

    https://github.com/apache/incubator-weex/pull/126#discussion_r106771757
  
    --- Diff: html5/frameworks/vanilla/index.js ---
    @@ -1,107 +1,334 @@
    -const config = {}
     
    -const instanceMap = {}
    +const instances = {}
    +const modules = {}
    +const components = {}
     
    -function init (cfg) {
    -  config.Document = cfg.Document
    -  config.Element = cfg.Element
    -  config.Comment = cfg.Comment
    -  config.sendTasks = cfg.sendTasks
    +const renderer = {
    +  instances,
    +  modules,
    +  components
     }
     
    -function registerComponents (components) {}
    +/**
    + * Prepare framework config, basically about the virtual-DOM and JS bridge.
    + * @param {object} cfg
    + */
    +export function init (cfg) {
    +  renderer.Document = cfg.Document
    +  renderer.Element = cfg.Element
    +  renderer.Comment = cfg.Comment
    +  renderer.sendTasks = cfg.sendTasks
    +}
     
    -function registerModules (modules) {}
    +/**
    + * Reset framework config and clear all registrations.
    + */
    +export function reset () {
    +  clear(instances)
    +  clear(modules)
    +  clear(components)
    +  delete renderer.Document
    +  delete renderer.Element
    +  delete renderer.Comment
    +  delete renderer.sendTasks
    +}
     
    -function registerMethods (apis) {}
    +/**
    + * Delete all keys of an object.
    + * @param {object} obj
    + */
    +function clear (obj) {
    +  for (const key in obj) {
    +    delete obj[key]
    +  }
    +}
     
    -function prepareInstance (id, options, data) {}
    +/**
    + * Create an instance with id, code, config and external data.
    + * @param {string} instanceId
    + * @param {string} appCode
    + * @param {object} config
    + * @param {object} data
    + * @param {object} env { info, config, services }
    + */
    +export function createInstance (
    +  instanceId,
    +  appCode = '',
    +  config = {},
    +  data,
    +  env = {}
    +) {
    +  // Virtual-DOM object.
    +  const document = new renderer.Document(instanceId, config.bundleUrl)
     
    -function createInstance (id, code, options, data, serviceObjects) {
    -  const document = new config.Document(id, options.bundleUrl)
    -  const callbacks = {}
    +  // 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.
    +  const callbacks = []
     
    -  let lastCallbackId = 0
    -  document.addCallback = func => {
    -    lastCallbackId++
    -    callbacks[lastCallbackId] = func
    -    return lastCallbackId
    -  }
    -  document.handleCallback = (funcId, data, ifLast) => {
    -    const callback = callbacks[funcId]
    -    if (ifLast) {
    -      delete callbacks[funcId]
    -    }
    -    return callback(data)
    +  // The latest callback id, incremental.
    +  const callbackId = 1
    +
    +  instances[instanceId] = {
    +    instanceId, config, data,
    +    document, callbacks, callbackId
       }
    -  instanceMap[id] = document
     
    -  const globalObjects = Object.assign({
    -    Document: config.Document,
    -    Element: config.Element,
    -    Comment: config.Comment,
    -    sendTasks: config.sendTasks,
    -    id, options, data, document
    -  }, serviceObjects)
    +  // Prepare native module getter and HTML5 Timer APIs.
    +  const moduleGetter = genModuleGetter(instanceId)
    +  const timerAPIs = getInstanceTimer(instanceId, moduleGetter)
     
    -  const globalKeys = []
    -  const globalValues = []
    -  for (const key in globalObjects) {
    -    globalKeys.push(key)
    -    globalValues.push(globalObjects[key])
    +  // Prepare `weex` instance variable.
    +  const weexInstanceVar = {
    +    config,
    +    document,
    +    requireModule: moduleGetter
       }
    -  globalKeys.push(code)
    +  Object.freeze(weexInstanceVar)
     
    -  const result = new Function(...globalKeys)
    -  return result(...globalValues)
    +  // The function which create a closure the JS Bundle will run in.
    +  // It will declare some instance variables like HTML5 Timer APIs etc.
    +  const instanceVars = Object.assign({
    +    weex: weexInstanceVar,
    +    // deprecated
    +    __weex_require_module__: weexInstanceVar.requireModule // 
eslint-disable-line
    --- End diff --
    
    Better have one in when people are using it frequently. Will do in Monday, 
the code is in my laptop at office.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to