Repository navigation
Expand file tree
/
Copy pathiconCache.js
More file actions
146 lines (116 loc) · 4.53 KB
/
Copy pathiconCache.js
File metadata and controls
146 lines (116 loc) · 4.53 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This file is part of the AppIndicator/KStatusNotifierItem GNOME Shell extension
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
const GLib = imports.gi.GLib
const GObject = imports.gi.GObject
const Mainloop = imports.mainloop
const Util = imports.misc.extensionUtils.getCurrentExtension().imports.util;
// The icon cache caches icon objects in case they're reused shortly aftwerwards.
// This is necessary for some indicators like skype which rapidly switch between serveral icons.
// Without caching, the garbage collection would never be able to handle the amount of new icon data.
// If the lifetime of an icon is over, the cache will destroy the icon. (!)
// The presence of an inUse property set to true on the icon will extend the lifetime.
const LIFETIME_TIMESPAN = 5000; // milli-seconds
const GC_INTERVAL = 10; // seconds
// how to use: see IconCache.add, IconCache.get
var IconCache = class AppIndicators_IconCache {
constructor() {
this._cache = {};
this._lifetime = {}; //we don't want to attach lifetime to the object
this._destroyNotify = {};
}
add(id, o) {
if (!(o && id))
return null;
if (!(id in this._cache) || this._cache[id] !== o) {
this._remove(id);
//Util.Logger.debug("IconCache: adding "+id,o);
this._cache[id] = o;
if ((o instanceof GObject.Object) && GObject.signal_lookup('destroy', o)) {
this._destroyNotify[id] = o.connect('destroy', () => {
this._remove(id);
});
}
}
this._renewLifetime(id);
this._checkGC();
return o;
}
_remove(id) {
if (!(id in this._cache))
return;
//Util.Logger.debug('IconCache: removing '+id);
let object = this._cache[id];
if ((object instanceof GObject.Object) && GObject.signal_lookup('destroy', object))
object.disconnect(this._destroyNotify[id]);
if (typeof object.destroy === 'function')
object.destroy();
delete this._cache[id];
delete this._lifetime[id];
delete this._destroyNotify[id];
this._checkGC();
}
_renewLifetime(id) {
if (id in this._cache)
this._lifetime[id] = new Date().getTime() + LIFETIME_TIMESPAN;
}
forceDestroy(id) {
this._remove(id);
}
// removes everything from the cache
clear() {
for (let id in this._cache)
this._remove(id)
this._checkGC();
}
// returns an object from the cache, or null if it can't be found.
get(id) {
if (id in this._cache) {
//Util.Logger.debug('IconCache: retrieving '+id);
this._renewLifetime(id);
return this._cache[id];
}
return null;
}
_checkGC() {
let cacheIsEmpty = (Object.keys(this._cache).length === 0);
if (!cacheIsEmpty && !this._gcTimeout) {
//Util.Logger.debug("IconCache: garbage collector started");
this._gcTimeout = Mainloop.timeout_add_seconds(GC_INTERVAL,
this._gc.bind(this));
} else if (cacheIsEmpty && this._gcTimeout) {
//Util.Logger.debug("IconCache: garbage collector stopped");
GLib.Source.remove(this._gcTimeout);
delete this._gcTimeout;
}
}
_gc() {
var time = new Date().getTime();
for (var id in this._cache) {
if (this._cache[id].inUse) {
//Util.Logger.debug("IconCache: " + id + " is in use.");
continue;
} else if (this._lifetime[id] < time) {
this._remove(id);
} else {
//Util.Logger.debug("IconCache: " + id + " survived this round.");
}
}
return true;
}
destroy() {
this.clear();
}
};