You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/06/16 04:05:57 UTC

[04/20] incubator-weex git commit: * [html5] update.

* [html5] update.


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

Branch: refs/heads/0.14-dev
Commit: afff3e4bc9c30030716fa0d1256ef06ba378ac91
Parents: cb39873
Author: MrRaindrop <te...@gmail.com>
Authored: Tue Jun 6 20:14:51 2017 +0800
Committer: MrRaindrop <te...@gmail.com>
Committed: Tue Jun 6 20:14:51 2017 +0800

----------------------------------------------------------------------
 html5/render/vue/modules/globalEvent.js | 35 +++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/afff3e4b/html5/render/vue/modules/globalEvent.js
----------------------------------------------------------------------
diff --git a/html5/render/vue/modules/globalEvent.js b/html5/render/vue/modules/globalEvent.js
index 66b7969..638f244 100644
--- a/html5/render/vue/modules/globalEvent.js
+++ b/html5/render/vue/modules/globalEvent.js
@@ -26,25 +26,44 @@ const handlerTraker = {}
 export default {
   /**
    * addEventListener
+   * NOTE: one callback can only be bound to the same event once. Bind a callback twice doesn't
+   *  mean it will be called twice when the event fired once.
    * @param {string} evt - the event name to add a listener on.
    */
   addEventListener (evt, callback) {
-    // const cb = e => this.sender.performCallback(callbackId, e)
-    const cb = e => callback && callback(e)
-    if (!handlerTraker[evt]) {
-      handlerTraker[evt] = [cb]
+    if (!callback) {
+      if (process.env.NODE_ENV === 'development') {
+        console.error(`[vue-render] missing callback arg in globalEvent.addEventListener.`)
+      }
+      return
     }
-    else {
-      handlerTraker.push(cb)
+    let handlers = handlerTraker[evt]
+    if (!handlers) {
+      handlers = handlerTraker[evt] = []
     }
-    document.addEventListener(evt, cb)
+    const len = handlers.length
+    for (let i = 0; i < len; i++) {
+      if (handlers[i] === callback) {
+        // this callback is already bound. no need to bind it again.
+        return
+      }
+    }
+    handlers.push(callback)
+    document.addEventListener(evt, callback)
   },
 
   /**
    * removeEventListener
+   * NOTE: remove all the event handlers for the specified event type.
    * @param {string} evt - the event name to remove a listener from.
    */
   removeEventListener (evt) {
-    handlerTraker[evt].forEach(cb => document.removeEventListener(evt, cb))
+    const handlers = handlerTraker[evt]
+    if (!handlers) {
+      // evt handlers has been already removed.
+      return
+    }
+    handlers.forEach(cb => document.removeEventListener(evt, cb))
+    delete handlerTraker[evt]
   }
 }