[Pkg-mozext-commits] [greasemonkey] 05/45: Use more robust way to keep the timer reference alive.

David Prévot taffit at moszumanska.debian.org
Mon Nov 3 20:59:18 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository greasemonkey.

commit 48eec5efbe92b523e4f35e7e29dfaaf516567e58
Author: Ventero <ventero at ventero.de>
Date:   Sat Aug 30 05:07:35 2014 +0200

    Use more robust way to keep the timer reference alive.
    
    Strict mode generates a warning about |timer;| being a useless
    expression, which suggest that this might be optimized away at some
    point (see also https://bugzil.la/640629#c9). Instead, explicitly
    store the timer in the observer object.
---
 modules/util/timeout.js | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/modules/util/timeout.js b/modules/util/timeout.js
index b3a9e2a..5ba3411 100644
--- a/modules/util/timeout.js
+++ b/modules/util/timeout.js
@@ -1,24 +1,23 @@
-Components.utils.import('resource://greasemonkey/util.js');
-
 const EXPORTED_SYMBOLS = ['timeout'];
 
-function timeout(aCallback, aDelay, aType) {
-  var type = aType;
-  if ('undefined' == typeof type) {
-    type = Components.interfaces.nsITimer.TYPE_ONE_SHOT;
-  }
-
+function timeout(aCallback, aDelay) {
   // Create the timer object.
   var timer = Components.classes["@mozilla.org/timer;1"]
       .createInstance(Components.interfaces.nsITimer);
-  // Init the callback, with a closure reference to the timer, so that it is
-  // not garbage collected before it fires.
-  timer.init(
-      {
-        'observe': function() {
-          timer;  // Here's the reference that keeps the timer alive!
-          aCallback();
-        }
-      },
-      aDelay, type);
+
+  // The timer object may be garbage collected before it fires, so we need to
+  // keep a reference to it alive (https://bugzil.la/647998).
+  // However, simply creating a closure over the timer object without using it
+  // may not be enough, as the timer might get optimized out of the closure
+  // scope (https://bugzil.la/640629#c9). To work around this, the timer object
+  // is explicitly stored as a property of the observer.
+  var observer = {
+    'observe': function() {
+      delete observer.timer;
+      aCallback();
+    },
+    'timer': timer
+  };
+
+  timer.init(observer, aDelay, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/greasemonkey.git



More information about the Pkg-mozext-commits mailing list