1
0
Fork 0

[Scheduler] Improve readability of some filters

The common conditions are now in the same order
master
Alessandro Cocco 2022-05-20 18:17:48 +02:00
parent 797eb12f7b
commit 531cf4b5f9
2 changed files with 18 additions and 15 deletions

View File

@ -8,12 +8,12 @@
var time = new Date();
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
var d = time.getDate();
var active = alarms.filter(
a=>a.on && // enabled
a.last!=d && // not already fired today
a.t+60000>currentTime && // is not in the past by >1 minute
(a.dow>>time.getDay())&1 && // is allowed on this day of the week
(!a.date || a.date==time.toISOString().substr(0,10)) // is allowed on this date
var active = alarms.filter(a =>
a.on // enabled
&& (a.last != d) // not already fired today
&& (a.t + 60000 > currentTime) // is not in the past by >1 minute
&& (a.dow >> time.getDay() & 1) // is allowed on this day of the week
&& (!a.date || a.date == time.toISOString().substr(0, 10)) // is allowed on this date
);
if (active.length) {
active = active.sort((a,b)=>a.t-b.t); // sort by time

View File

@ -11,16 +11,19 @@ exports.getAlarm = function(id) {
return exports.getAlarms().find(a=>a.id==id);
};
// Given a list of alarms from getAlarms, return a list of active alarms for the given time (or current time if time not specified)
exports.getActiveAlarms = function(alarms, time) {
exports.getActiveAlarms = function (alarms, time) {
if (!time) time = new Date();
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000)
+10000;// get current time - 10s in future to ensure we alarm if we've started the app a tad early
return alarms.filter(a=>a.on
&&(a.t<currentTime)
&&(a.last!=time.getDate())
&& (!a.date || a.date==time.toISOString().substr(0,10))
&& a.dow >> (time).getDay() & 1)
.sort((a,b)=>a.t-b.t);
// get current time 10s in future to ensure we alarm if we've started the app a tad early
var currentTime = (time.getHours() * 3600000) + (time.getMinutes() * 60000) + (time.getSeconds() * 1000) + 10000;
return alarms
.filter(a =>
a.on // enabled
&& (a.last != time.getDate()) // not already fired today
&& (a.t < currentTime)
&& (a.dow >> time.getDay() & 1) // is allowed on this day of the week
&& (!a.date || a.date == time.toISOString().substr(0, 10)) // is allowed on this date
)
.sort((a, b) => a.t - b.t);
}
// Set an alarm object based on ID. Leave 'alarm' undefined to remove it
exports.setAlarm = function(id, alarm) {