gecko/toolkit/modules/DeferredTask.jsm
Ekanan Ketunuti 620410f94c Bug 828116 - Move modules in toolkit/content and toolkit/mozapps/shared to toolkit/modules. r=Mossop
--HG--
rename : toolkit/mozapps/shared/CertUtils.jsm => toolkit/modules/CertUtils.jsm
rename : toolkit/content/DeferredTask.jsm => toolkit/modules/DeferredTask.jsm
rename : toolkit/content/Deprecated.jsm => toolkit/modules/Deprecated.jsm
rename : toolkit/content/Dict.jsm => toolkit/modules/Dict.jsm
rename : toolkit/mozapps/shared/FileUtils.jsm => toolkit/modules/FileUtils.jsm
rename : toolkit/content/Geometry.jsm => toolkit/modules/Geometry.jsm
rename : toolkit/content/InlineSpellChecker.jsm => toolkit/modules/InlineSpellChecker.jsm
rename : toolkit/content/LightweightThemeConsumer.jsm => toolkit/modules/LightweightThemeConsumer.jsm
rename : toolkit/content/PageMenu.jsm => toolkit/modules/PageMenu.jsm
rename : toolkit/content/PopupNotifications.jsm => toolkit/modules/PopupNotifications.jsm
rename : toolkit/content/PrivateBrowsingUtils.jsm => toolkit/modules/PrivateBrowsingUtils.jsm
rename : toolkit/content/PropertyListUtils.jsm => toolkit/modules/PropertyListUtils.jsm
rename : toolkit/content/Services.jsm => toolkit/modules/Services.jsm
rename : toolkit/content/Task.jsm => toolkit/modules/Task.jsm
rename : toolkit/content/Troubleshoot.jsm => toolkit/modules/Troubleshoot.jsm
rename : toolkit/content/UpdateChannel.jsm => toolkit/modules/UpdateChannel.jsm
rename : toolkit/content/WindowDraggingUtils.jsm => toolkit/modules/WindowDraggingUtils.jsm
rename : toolkit/content/debug.js => toolkit/modules/debug.js
rename : toolkit/content/tests/browser/browser_DeferredTask.js => toolkit/modules/tests/browser/browser_DeferredTask.js
rename : toolkit/content/tests/browser/browser_Deprecated.js => toolkit/modules/tests/browser/browser_Deprecated.js
rename : toolkit/content/tests/browser/browser_Geometry.js => toolkit/modules/tests/browser/browser_Geometry.js
rename : toolkit/content/tests/browser/browser_InlineSpellChecker.js => toolkit/modules/tests/browser/browser_InlineSpellChecker.js
rename : toolkit/content/tests/browser/browser_Services.js => toolkit/modules/tests/browser/browser_Services.js
rename : toolkit/content/tests/browser/browser_Troubleshoot.js => toolkit/modules/tests/browser/browser_Troubleshoot.js
rename : toolkit/mozapps/shared/test/chrome/Makefile.in => toolkit/modules/tests/chrome/Makefile.in
rename : toolkit/mozapps/shared/test/chrome/moz.build => toolkit/modules/tests/chrome/moz.build
rename : toolkit/mozapps/shared/test/chrome/test_bug544442_checkCert.xul => toolkit/modules/tests/chrome/test_bug544442_checkCert.xul
rename : toolkit/content/tests/unit/propertyLists/bug710259_propertyListBinary.plist => toolkit/modules/tests/xpcshell/propertyLists/bug710259_propertyListBinary.plist
rename : toolkit/content/tests/unit/propertyLists/bug710259_propertyListXML.plist => toolkit/modules/tests/xpcshell/propertyLists/bug710259_propertyListXML.plist
rename : toolkit/mozapps/shared/test/unit/test_FileUtils.js => toolkit/modules/tests/xpcshell/test_FileUtils.js
rename : toolkit/content/tests/unit/test_dict.js => toolkit/modules/tests/xpcshell/test_dict.js
rename : toolkit/content/tests/unit/test_propertyListsUtils.js => toolkit/modules/tests/xpcshell/test_propertyListsUtils.js
rename : toolkit/mozapps/shared/test/unit/test_readCertPrefs.js => toolkit/modules/tests/xpcshell/test_readCertPrefs.js
rename : toolkit/content/tests/unit/test_task.js => toolkit/modules/tests/xpcshell/test_task.js
2013-05-14 14:37:18 -07:00

86 lines
2.1 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
this.EXPORTED_SYMBOLS = ["DeferredTask"];
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
/**
* A task that will run after a delay. Multiple attempts to run the same task
* before the delay will be coalesced
*
* Use this for instance if you write data to a file and you expect that you
* may have to rewrite data very soon afterwards. With |DeferredTask|, the
* task is delayed by a few milliseconds and, should a new change to the data
* occur during this period,
* 1/ only the final version of the data is actually written;
* 2/ a further grace delay is added to take into account other changes.
*
* Constructor
* @param aDelay The delay time in milliseconds.
* @param aCallback The code to execute after the delay.
*/
this.DeferredTask = function DeferredTask(aCallback, aDelay) {
this._callback = function onCallback() {
this._timer = null;
try {
aCallback();
} catch(e) {
Cu.reportError(e);
}
}.bind(this);
this._delay = aDelay;
}
DeferredTask.prototype = {
/* Callback */
_callback: null,
/* Delay */
_delay: null,
/* Timer */
_timer: null,
/**
* Check the current task state.
* @returns true if pending, false otherwise.
*/
get isPending() {
return (this._timer != null);
},
/**
* Start (or postpone) task.
*/
start: function DeferredTask_start() {
if (this._timer) {
this._timer.cancel();
}
this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
this._timer.initWithCallback(
this._callback, this._delay, Ci.nsITimer.TYPE_ONE_SHOT);
},
/**
* Perform any postponed task immediately.
*/
flush: function DeferredTask_flush() {
if (this._timer) {
this.cancel();
this._callback();
}
},
/**
* Cancel any pending task.
*/
cancel: function DeferredTask_cancel() {
if (this._timer) {
this._timer.cancel();
this._timer = null;
}
}
};