mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-21 09:08:10 +01:00
Periodically save storage objects
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user