stylus/schedule/core.js

120 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2017-05-03 12:21:15 +00:00
/* globals getStylesSafe, saveStyleSafe, BG */
'use strict';
var SCHEDULE_PREFIX = 'schedule';
var schedule = {};
schedule.prefs = {
2017-05-03 14:49:40 +00:00
name (id) {
return SCHEDULE_PREFIX + '.' + id;
},
validate (name) {
return name.startsWith(SCHEDULE_PREFIX + '.');
},
2017-05-03 12:21:15 +00:00
get (name, callback) {
chrome.storage.local.get(name, callback);
},
getAll (callback) {
schedule.prefs.get(null, prefs => {
callback(
2017-05-03 14:49:40 +00:00
Object.keys(prefs).filter(schedule.prefs.validate)
2017-05-03 12:21:15 +00:00
.map(n => [n, prefs[n]])
);
});
},
set (name, value, callback = () => {}) {
chrome.storage.local.set({
[name]: value
}, callback);
},
remove (name) {
chrome.storage.local.remove(name);
},
subscribe (callback) {
chrome.storage.onChanged.addListener(prefs => {
Object.keys(prefs)
.filter(n => prefs[n].newValue)
.forEach(n => callback(n, prefs[n].newValue));
});
}
};
2017-05-03 14:49:40 +00:00
/* call this function when schedule timing is modified; if timing is not modified, nothing happens */
2017-05-03 12:21:15 +00:00
schedule.entry = request => {
2017-05-03 14:49:40 +00:00
console.log('schedule.entry', 'schedule timing might have been changed', request);
2017-05-03 12:21:15 +00:00
return new Promise((resolve, reject) => {
chrome.permissions.request({
permissions: ['idle', 'alarms']
}, (granted) => {
if (granted) {
2017-05-03 14:49:40 +00:00
schedule.prefs.set(schedule.prefs.name(request.id), {
2017-05-03 12:21:15 +00:00
id: request.id,
start: request.start,
end: request.end,
enabled: request.enabled
2017-05-03 14:49:40 +00:00
}, resolve);
2017-05-03 12:21:15 +00:00
}
else {
reject(new Error('Required permissions are not granted'));
}
});
});
};
2017-05-03 14:49:40 +00:00
/* call this to update current alarm. If request.enabled = false, then alarm is cleared and this job will be removed from the storage */
2017-05-03 12:21:15 +00:00
schedule.execute = (name, request) => {
2017-05-03 14:49:40 +00:00
console.log('schedule.execute', 'evaluating response', name, request);
2017-05-03 12:21:15 +00:00
chrome.alarms.clear(name, () => {
if (request.enabled) {
const now = new Date();
let start = new Date(now.toDateString() + ' ' + request.start).getTime() - now;
let end = new Date(now.toDateString() + ' ' + request.end).getTime() - now;
2017-05-03 14:49:40 +00:00
const when = now.getTime() + Math.min(
start < 0 ? start + 24 * 60 * 60 * 1000 : start,
end < 0 ? end + 24 * 60 * 60 * 1000 : end
);
console.log(`next alarm is set for id = ${request.id}`, new Date(when), start, end);
chrome.alarms.create(name, {when});
2017-05-03 12:21:15 +00:00
getStylesSafe({id: request.id}).then(([style]) => {
if (style) {
const enabled = (start <= 0 && end > 0) || (start > end && start * end > 0) ;
2017-05-03 14:49:40 +00:00
console.log(`style with id = ${style.id}; enabled = `, enabled);
2017-05-03 12:21:15 +00:00
saveStyleSafe({
id: request.id,
enabled
});
}
else {
// clear schedule if style is not found
2017-05-03 14:49:40 +00:00
console.log('removing from storage since style is not found', request);
2017-05-03 12:21:15 +00:00
schedule.execute(name, Object.assign(
request, {enabled: false}
));
}
});
}
else {
2017-05-03 14:49:40 +00:00
console.log('removing pref since request.enabled is false', name);
2017-05-03 12:21:15 +00:00
schedule.prefs.remove(name);
}
});
};
// background only
if (BG === window) {
2017-05-03 14:49:40 +00:00
// listen for pref changes to update chrome.alarms
schedule.prefs.subscribe((name, pref) => schedule.prefs.validate(name) && schedule.execute(name, pref));
2017-05-03 12:21:15 +00:00
chrome.alarms.onAlarm.addListener(({name}) => {
2017-05-03 14:49:40 +00:00
schedule.prefs.get(name, prefs => prefs[name] && schedule.execute(name, prefs[name]));
2017-05-03 12:21:15 +00:00
});
(function (callback) {
2017-05-03 14:49:40 +00:00
chrome.idle.onStateChanged.addListener(state => state === 'active' && callback());
2017-05-03 12:21:15 +00:00
window.setTimeout(callback);
})(function () {
2017-05-03 14:49:40 +00:00
console.log('updating all schedules');
2017-05-03 12:21:15 +00:00
schedule.prefs.getAll(prefs => prefs.forEach(a => schedule.execute(...a)));
});
}