From e9cb763f374ef6f0e31a7f5d281c40e5eae9e36e Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Wed, 10 Dec 2008 16:17:41 -0500 Subject: [PATCH] Periodically save storage objects --- common/content/completion.js | 2 +- common/content/ui.js | 6 ++-- common/modules/storage.jsm | 60 +++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/common/content/completion.js b/common/content/completion.js index 85ae8512..849530dc 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -1404,7 +1404,7 @@ function Completion() //{{{ context.hasItems = context.completions.length > 0; // XXX context.filterFunc = null; context.compare = null; - let timer = new util.Timer(50, 100, function (result) { + let timer = new Timer(50, 100, function (result) { context.incomplete = result.searchResult >= result.RESULT_NOMATCH_ONGOING; context.completions = [ [result.getValueAt(i), result.getCommentAt(i), result.getImageAt(i)] diff --git a/common/content/ui.js b/common/content/ui.js index 882cf35c..5a8cf656 100644 --- a/common/content/ui.js +++ b/common/content/ui.js @@ -408,21 +408,21 @@ function CommandLine() //{{{ ////////////////////// TIMERS ////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ - var statusTimer = new util.Timer(5, 100, function statusTell() { + var statusTimer = new Timer(5, 100, function statusTell() { if (completions.selected == null) statusline.updateProgress(""); else statusline.updateProgress("match " + (completions.selected + 1) + " of " + completions.items.length); }); - var autocompleteTimer = new util.Timer(201, 300, function autocompleteTell(tabPressed) { + var autocompleteTimer = new Timer(201, 300, function autocompleteTell(tabPressed) { if (events.feedingKeys || !completions) return; completions.complete(true, false); completions.itemList.show(); }); - var tabTimer = new util.Timer(0, 0, function tabTell(event) { + var tabTimer = new Timer(0, 0, function tabTell(event) { if (completions) completions.tab(event.shiftKey); }); diff --git a/common/modules/storage.jsm b/common/modules/storage.jsm index e3f1aea8..ffb96d2d 100644 --- a/common/modules/storage.jsm +++ b/common/modules/storage.jsm @@ -27,7 +27,61 @@ the provisions above, a recipient may use your version of this file under the terms of any one of the MPL, the GPL or the LGPL. }}} ***** END LICENSE BLOCK *****/ -var EXPORTED_SYMBOLS = ["storage"]; +var EXPORTED_SYMBOLS = ["storage", "Timer"]; + +function Timer(minInterval, maxInterval, callback) +{ + let timer = Components.classes["@mozilla.org/timer;1"] + .createInstance(Components.interfaces.nsITimer); + this.doneAt = 0; + this.latest = 0; + this.notify = function (aTimer) + { + timer.cancel(); + this.latest = 0; + /* minInterval is the time between the completion of the command and the next firing. */ + this.doneAt = Date.now() + minInterval; + + try + { + callback(this.arg); + } + finally + { + this.doneAt = Date.now() + minInterval; + } + }; + this.tell = function (arg) + { + if (arg !== undefined) + this.arg = arg; + + let now = Date.now(); + if (this.doneAt == -1) + timer.cancel(); + + let timeout = minInterval; + if (now > this.doneAt && this.doneAt > -1) + timeout = 0; + else if (this.latest) + timeout = Math.min(timeout, this.latest - now); + else + this.latest = now + maxInterval; + + timer.initWithCallback(this, Math.max(timeout, 0), timer.TYPE_ONE_SHOT); + this.doneAt = -1; + }; + this.reset = function () + { + timer.cancel(); + this.doneAt = 0; + }; + this.flush = function () + { + if (this.latest) + this.notify(); + }; +} var prefService = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefService) @@ -159,6 +213,7 @@ function ArrayStore(name, store, data) var funcName = aFuncName; arguments[0] = array; array = Array[funcName].apply(Array, arguments); + this.fireEvent("change", null); }; this.get = function get(index) @@ -172,6 +227,7 @@ ArrayStore.prototype = prototype; var keys = {}; var observers = {}; +var timers = {}; var storage = { newObject: function newObject(key, constructor, store, type, reload) @@ -181,6 +237,7 @@ var storage = { if (key in this && !reload) throw Error; keys[key] = new constructor(key, store, loadPref(key, store, type || Object)); + timers[key] = new Timer(1000, 10000, function () storage.save(key)); this.__defineGetter__(key, function () keys[key]); } return keys[key]; @@ -217,6 +274,7 @@ var storage = { { for each (callback in observers[key]) callback(key, event, arg); + timers[key].tell(); }, save: function save(key)