-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwatch.browser.js
More file actions
59 lines (47 loc) · 1.24 KB
/
Copy pathwatch.browser.js
File metadata and controls
59 lines (47 loc) · 1.24 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
var read = require('./read');
var update = require('./update');
var notify = require('./notify');
var ulogs = new Set();
var minPollTime = 350;
var maxPollTime = 5000;
var defaultPollTime = 350;
module.exports = function(ulog) {
var delay = defaultPollTime;
var intervalId = null;
if(!ulogs.has(ulog)) {
// storage events unfortunately only fire on events triggered by other windows...
// so we need to poll here...
startPolling();
ulogs.add(ulog);
}
return stopPolling;
function stopPolling() {
if(intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
}
function startPolling(){
intervalId = setInterval(pollStorageConfig, delay)
}
function pollStorageConfig(){
if (ulog.config) {
var cfg = read(ulog);
var changed = update(ulog.config, cfg);
if (changed.length) {
notify(ulog, changed)
}
var newDelay = parseInt(ulog.get('poll_time'), 10) || defaultPollTime;
if(newDelay < minPollTime) {
newDelay = minPollTime;
} else if(newDelay > maxPollTime) {
newDelay = maxPollTime;
}
if (newDelay !== delay) {
delay = newDelay;
stopPolling();
startPolling();
}
}
}
};