summaryrefslogtreecommitdiff
path: root/src/plugins/thunderbird/chrome/content/queue.js
blob: c3f0b817b07655b60ebc520f0dc9d62e34d12262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
org.bustany.TrackerBird.Queue = function(delay) {
	this._ui = org.bustany.TrackerBird.Ui;
	this.__console = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
	this._log = function(msg) {
	   this.__console.logStringMessage(msg);
	}

	this._delay = delay;
	this._items = [];
	this._active = false;

	var queue = this;
	this._timerEvent = { notify: function(timer) { queue._active = false; queue.process(); } };
	this._queueTimer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
	dump("Trackerbird created queue with delay " + delay + "\n");
}

org.bustany.TrackerBird.Queue.prototype.add = function(item) {
	this._items.push(item);
	this.process();
}

org.bustany.TrackerBird.Queue.prototype.addImmediate = function(item) {
	this._items.unshift(item);
	this.process();
}

org.bustany.TrackerBird.Queue.prototype.process = function() {
	if (this._items.length == 0) {
		this._ui.showMessage("Indexer idle");
		return;
	}
	this._ui.showMessage(this._items.length + " actions remaining");

	if (this._active) {
		return;
	}
	this._active = true;

	var item = this._items.shift();

	try {
		item.callback(item.data);
	} catch (ex) {
		dump ("Trackbird could not execute: " + ex + "\n");
		this._log("Trackerbird could not execute: " + ex);
	}

	this._queueTimer.initWithCallback(this._timerEvent, this._delay, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
}

org.bustany.TrackerBird.Queue.prototype.size = function() {
	return this._items.length;
}