You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by gu...@apache.org on 2017/11/09 08:09:52 UTC

[1/2] incubator-weex git commit: + [jsfm] support register and trigger component hooks

Repository: incubator-weex
Updated Branches:
  refs/heads/master 2fd673115 -> 1f63e1acd


+ [jsfm] support register and trigger component hooks


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

Branch: refs/heads/master
Commit: b9f8b876746cfa2d8661fd141bda24f2e1086f72
Parents: 5c78438
Author: Hanks <zh...@gmail.com>
Authored: Thu Nov 9 15:14:35 2017 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Thu Nov 9 15:14:35 2017 +0800

----------------------------------------------------------------------
 html5/runtime/bridge/CallbackManager.js | 31 ++++++++++++++++++++++++++++
 html5/runtime/bridge/TaskCenter.js      |  8 +++++++
 html5/runtime/bridge/receiver.js        | 16 ++++++++++++++
 3 files changed, 55 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/b9f8b876/html5/runtime/bridge/CallbackManager.js
----------------------------------------------------------------------
diff --git a/html5/runtime/bridge/CallbackManager.js b/html5/runtime/bridge/CallbackManager.js
index 058fcc0..4c38fdf 100644
--- a/html5/runtime/bridge/CallbackManager.js
+++ b/html5/runtime/bridge/CallbackManager.js
@@ -19,6 +19,10 @@
 
 import { decodePrimitive } from './normalize'
 
+function getHookKey (componentId, type, hookName) {
+  return `${type}@${hookName}#${componentId}`
+}
+
 /**
  * For general callback management of a certain Weex instance.
  * Because function can not passed into native, so we create callback
@@ -31,6 +35,7 @@ export default class CallbackManager {
     this.instanceId = instanceId
     this.lastCallbackId = 0
     this.callbacks = {}
+    this.hooks = {}
   }
   add (callback) {
     this.lastCallbackId++
@@ -42,6 +47,31 @@ export default class CallbackManager {
     delete this.callbacks[callbackId]
     return callback
   }
+  registerHook (componentId, type, hookName, hookFunction) {
+    // TODO: validate arguments
+    const key = getHookKey(componentId, type, hookName)
+    if (this.hooks[key]) {
+      console.warn(`[JS Framework] Override an existing component hook "${key}".`)
+    }
+    this.hooks[key] = hookFunction
+  }
+  triggerHook (componentId, type, hookName, options = {}) {
+    // TODO: validate arguments
+    const key = getHookKey(componentId, type, hookName)
+    const hookFunction = this.hooks[key]
+    if (typeof hookFunction === 'function') {
+      console.error(`[JS Framework] Invalid hook function type (${typeof hookFunction}) on "${key}".`)
+      return null
+    }
+    let result = null
+    try {
+      result = hookFunction.apply(null, options.args || [])
+    }
+    catch (e) {
+      console.error(`[JS Framework] Failed to execute the hook function on "${key}".`)
+    }
+    return result
+  }
   consume (callbackId, data, ifKeepAlive) {
     const callback = this.callbacks[callbackId]
     if (typeof ifKeepAlive === 'undefined' || ifKeepAlive === false) {
@@ -54,5 +84,6 @@ export default class CallbackManager {
   }
   close () {
     this.callbacks = {}
+    this.hooks = {}
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/b9f8b876/html5/runtime/bridge/TaskCenter.js
----------------------------------------------------------------------
diff --git a/html5/runtime/bridge/TaskCenter.js b/html5/runtime/bridge/TaskCenter.js
index 0479df8..d2350fc 100644
--- a/html5/runtime/bridge/TaskCenter.js
+++ b/html5/runtime/bridge/TaskCenter.js
@@ -42,6 +42,14 @@ export class TaskCenter {
     return this.callbackManager.consume(callbackId, data, ifKeepAlive)
   }
 
+  registerHook (...args) {
+    return this.callbackManager.registerHook(...args)
+  }
+
+  triggerHook (...args) {
+    return this.callbackManager.triggerHook(...args)
+  }
+
   destroyCallback () {
     return this.callbackManager.close()
   }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/b9f8b876/html5/runtime/bridge/receiver.js
----------------------------------------------------------------------
diff --git a/html5/runtime/bridge/receiver.js b/html5/runtime/bridge/receiver.js
index d137a26..01aba56 100644
--- a/html5/runtime/bridge/receiver.js
+++ b/html5/runtime/bridge/receiver.js
@@ -31,6 +31,21 @@ function callback (document, callbackId, data, ifKeepAlive) {
   return document.taskCenter.callback(callbackId, data, ifKeepAlive)
 }
 
+function componentHook (document, componentId, type, hook, options) {
+  if (!document || !document.taskCenter) {
+    console.error(`[JS Framework] Can't find "document" or "taskCenter".`)
+    return null
+  }
+  let result = null
+  try {
+    result = document.taskCenter.triggerHook(componentId, type, hook, options)
+  }
+  catch (e) {
+    console.error(`[JS Framework] Failed to trigger the "${type}@${hook}" hook on ${componentId}.`)
+  }
+  return result
+}
+
 /**
  * Accept calls from native (event or callback).
  *
@@ -47,6 +62,7 @@ export function receiveTasks (id, tasks) {
       switch (task.method) {
         case 'callback': return callback(document, ...task.args)
         case 'fireEvent': return fireEvent(document, ...task.args)
+        case 'componentHook': return componentHook(document, ...task.args)
       }
     })
   }


[2/2] incubator-weex git commit: * [jsfm] Support register and trigger component hooks

Posted by gu...@apache.org.
* [jsfm] Support register and trigger component hooks


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

Branch: refs/heads/master
Commit: 1f63e1acd36f52f354f01024d28ec05b836cd592
Parents: 2fd6731 b9f8b87
Author: gurisxie <27...@qq.com>
Authored: Thu Nov 9 16:09:15 2017 +0800
Committer: gurisxie <27...@qq.com>
Committed: Thu Nov 9 16:09:41 2017 +0800

----------------------------------------------------------------------
 html5/runtime/bridge/CallbackManager.js | 31 ++++++++++++++++++++++++++++
 html5/runtime/bridge/TaskCenter.js      |  8 +++++++
 html5/runtime/bridge/receiver.js        | 16 ++++++++++++++
 3 files changed, 55 insertions(+)
----------------------------------------------------------------------