Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable polling timeout and bugfix in watch.browser #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mods/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ var notify = require('./notify')
var watch = require('./watch')
var config = module.exports = {
use: [
require('../settings'),
require('../settings')
],

settings: {
config: {
config: 'log_config'
},
pollTime: {
config: 'poll_time',
prop: {
default: 350
}
}
},

Expand Down Expand Up @@ -42,5 +48,9 @@ var config = module.exports = {

set: function(name) {
if (name === 'log_config') config.update(this)
},

after: function () {
config.update(this);
}
}
70 changes: 55 additions & 15 deletions mods/config/watch.browser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
var read = require('./read')
var update = require('./update')
var notify = require('./notify')
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) {
// storage events unfortunately only fire on events triggered by other windows...
// so we need to poll here...
setInterval(function(){
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)
setTimeout(function(){
var changed = update(ulog.config, cfg)
if (changed.length) setTimeout(function(){
notify(ulog, changed)
}, 0)
}, 0)
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();
}
}
}, 350)
}
}
};
2 changes: 2 additions & 0 deletions mods/levels/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ test('mod: levels', function (t) {

t.test('levels.ext(logger: Function)', function (t) {
ulog.use(levels)
//simple workaround to fix test problem in pull request #58
ulog.set('level', 'info')
var logger = ulog('test')
t.equal(logger.NONE, 0, 'creates a property `NONE`')
for (lvl in ulog.levels) {
Expand Down