aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/async.js
blob: 9debc58a281484b442514abf44c76f9273297306 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*******************************************************************************

    µBlock - a browser extension to block requests.
    Copyright (C) 2014 Raymond Hill

    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 3 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, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

/* global µBlock */
'use strict';

/******************************************************************************/

// Async job queue module

µBlock.asyncJobs = (function() {

/******************************************************************************/

var processJobs = function() {
    asyncJobManager.process();
};

var AsyncJobEntry = function(name) {
    this.name = name;
    this.data = null;
    this.callback = null;
    this.when = 0;
    this.period = 0;
};

AsyncJobEntry.prototype.destroy = function() {
    this.name = '';
    this.data = null;
    this.callback = null;
};

/******************************************************************************/

var AsyncJobManager = function() {
    this.timeResolution = 200;
    this.jobs = {};
    this.jobCount = 0;
    this.jobJunkyard = [];
    this.timerId = null;
    this.timerWhen = Number.MAX_VALUE;
};

/******************************************************************************/

AsyncJobManager.prototype.restartTimer = function() {
    // TODO: Another way to do this is to extract the keys, sort the keys
    // in chronological order, than pick the first entry to get the next
    // time at which we want a time event to fire. Completely unsure the
    // overhead of extracting keys/sorting is less than what is below.
    // I could also keep the keys ordered, and use binary search when adding
    // a new job.
    var when = Number.MAX_VALUE;
    var jobs = this.jobs, job;
    for ( var jobName in jobs ) {
        job = jobs[jobName];
        if ( job instanceof AsyncJobEntry ) {
            if ( job.when < when ) {
                when = job.when;
            }
        }
    }
    // Quantize time value
    when = Math.floor((when + this.timeResolution - 1) / this.timeResolution) * this.timeResolution;

    // TODO: Maybe use chrome.alarms() API when the next job is at more than
    // one minute in the future... From reading about it, chrome.alarms() is
    // smarter in that it will fire the event only when the browser is not
    // too busy.
    if ( when < this.timerWhen ) {
        clearTimeout(this.timerId);
        this.timerWhen = when;
        this.timerId = vAPI.setTimeout(processJobs, Math.max(when - Date.now(), 10));
    }
};

/******************************************************************************/

AsyncJobManager.prototype.add = function(name, data, callback, delay, recurrent) {
    var job = this.jobs[name];
    if ( !job ) {
        job = this.jobJunkyard.pop();
        if ( !job ) {
            job = new AsyncJobEntry(name);
        } else {
            job.name = name;
        }
        this.jobs[name] = job;
        this.jobCount++;
    }
    job.data = data;
    job.callback = callback;
    job.when = Date.now() + delay;
    job.period = recurrent ? delay : 0;
    this.restartTimer();
};

/******************************************************************************/

AsyncJobManager.prototype.remove = function(jobName) {
    if ( this.jobs.hasOwnProperty(jobName) === false ) {
        return;
    }
    var job = this.jobs[jobName];
    delete this.jobs[jobName];
    job.destroy();
    this.jobCount--;
    this.jobJunkyard.push(job);
    this.restartTimer();
};

/******************************************************************************/

AsyncJobManager.prototype.process = function() {
    this.timerId = null;
    this.timerWhen = Number.MAX_VALUE;
    var now = Date.now();
    var job;
    for ( var jobName in this.jobs ) {
        if ( this.jobs.hasOwnProperty(jobName) === false ) {
            continue;
        }
        job = this.jobs[jobName];
        if ( job.when > now ) {
            continue;
        }
        job.callback(job.data);
        if ( job.period ) {
            job.when = now + job.period;
        } else {
            delete this.jobs[jobName];
            job.destroy();
            this.jobCount--;
            this.jobJunkyard.push(job);
        }
    }
    this.restartTimer();
};

/******************************************************************************/

// Only one instance
var asyncJobManager = new AsyncJobManager();

/******************************************************************************/

// Publish
return asyncJobManager;

})();

/******************************************************************************/

// Update visual of extension icon.

µBlock.updateBadgeAsync = (function() {
    var tabIdToTimer = Object.create(null);

    var updateBadge = function(tabId) {
        delete tabIdToTimer[tabId];

        var state = false;
        var badge = '';

        var pageStore = this.pageStoreFromTabId(tabId);
        if ( pageStore !== null ) {
            state = pageStore.getNetFilteringSwitch();
            if ( state && this.userSettings.showIconBadge && pageStore.perLoadBlockedRequestCount ) {
                badge = this.utils.formatCount(pageStore.perLoadBlockedRequestCount);
            }
        }

        vAPI.setIcon(tabId, state ? 'on' : 'off', badge);
    };

    return function(tabId) {
        if ( tabIdToTimer[tabId] ) {
            return;
        }
        if ( vAPI.isBehindTheSceneTabId(tabId) ) {
            return;
        }
        tabIdToTimer[tabId] = vAPI.setTimeout(updateBadge.bind(this, tabId), 500);
    };
})();