File: iconCache.js

package info (click to toggle)
gnome-shell-extension-appindicator 34-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 264 kB
  • sloc: javascript: 2,015; xml: 147; python: 55; makefile: 11
file content (140 lines) | stat: -rw-r--r-- 4,490 bytes parent folder | download
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
// 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 Gio = imports.gi.Gio

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 GC_INTERVAL = 60; // seconds
const LIFETIME_TIMESPAN = 10; // seconds

// how to use: see IconCache.add, IconCache.get
var IconCache = class AppIndicators_IconCache {
    constructor() {
        this._cache = new Map();
        this._lifetime = new Map(); //we don't want to attach lifetime to the object
    }

    add(id, icon) {
        if (!(icon instanceof Gio.Icon)) {
            Util.Logger.critical('IconCache: Only Gio.Icons are supported');
            return null;
        }

        if (!id) {
            Util.Logger.critical('IconCache: Invalid ID provided');
            return null;
        }

        let oldIcon = this._cache.get(id);
        if (!oldIcon || !oldIcon.equals(icon)) {
            Util.Logger.debug(`IconCache: adding ${id}: ${icon}`);
            this._cache.set(id, icon);
        } else {
            icon = oldIcon;
        }

        this._renewLifetime(id);
        this._checkGC();

        return icon;
    }

    _remove(id) {
        Util.Logger.debug(`IconCache: removing ${id}`);

        this._cache.get(id).run_dispose();
        this._cache.delete(id);
        this._lifetime.delete(id);
    }

    _renewLifetime(id) {
        this._lifetime.set(id, new Date().getTime() + LIFETIME_TIMESPAN * 1000);
    }

    forceDestroy(id) {
        if (this._cache.has(id)) {
            this._remove(id);
            this._checkGC();
        }
    }

    // marks all the icons as removable, if something doesn't claim them before
    weakClear() {
        this._cache.forEach((icon) => icon.inUse = false);
        this._checkGC();
    }

    // removes everything from the cache
    clear() {
        this._cache.forEach((_icon, id) => this._remove(id));
        this._checkGC();
    }

    // returns an object from the cache, or null if it can't be found.
    get(id) {
        let icon = this._cache.get(id);
        if (icon) {
            Util.Logger.debug(`IconCache: retrieving ${id}: ${icon}`);
            this._renewLifetime(id);
            return icon;
        }

        return null;
    }

    _checkGC() {
        let cacheIsEmpty = this._cache.size == 0;

        if (!cacheIsEmpty && !this._gcTimeout) {
            Util.Logger.debug("IconCache: garbage collector started");
            this._gcTimeout = GLib.timeout_add_seconds(
                GLib.PRIORITY_LOW,
                GC_INTERVAL,
                () => this._gc());
        } else if (cacheIsEmpty && this._gcTimeout) {
            Util.Logger.debug("IconCache: garbage collector stopped");
            GLib.Source.remove(this._gcTimeout);
            delete this._gcTimeout;
        }
    }

    _gc() {
        let time = new Date().getTime();
        this._cache.forEach((icon, id) => {
            if (icon.inUse) {
                Util.Logger.debug(`IconCache: ${id} is in use.`);
            } else if (this._lifetime.get(id) < time) {
                this._remove(id);
            } else {
                Util.Logger.debug(`IconCache: ${id} survived this round.`);
            }
        });

        return true;
    }

    destroy() {
        this.clear();
    }
};