You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-06 09:19:22 +02:00
Desktop: Fixes #1829: Fixed alarms that would trigger immediately when they were set too far in future
This commit is contained in:
@@ -4,6 +4,8 @@ const Alarm = require('lib/models/Alarm.js');
|
||||
class AlarmService {
|
||||
static setDriver(v) {
|
||||
this.driver_ = v;
|
||||
|
||||
if (this.driver_.setService) this.driver_.setService(this);
|
||||
}
|
||||
|
||||
static driver() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const notifier = require('node-notifier');
|
||||
const { bridge } = require('electron').remote.require('./bridge');
|
||||
|
||||
class AlarmServiceDriverNode {
|
||||
constructor(options) {
|
||||
@@ -6,6 +7,15 @@ class AlarmServiceDriverNode {
|
||||
// https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
|
||||
this.appName_ = options.appName;
|
||||
this.notifications_ = {};
|
||||
this.service_ = null;
|
||||
}
|
||||
|
||||
setService(s) {
|
||||
this.service_ = s;
|
||||
}
|
||||
|
||||
logger() {
|
||||
return this.service_.logger();
|
||||
}
|
||||
|
||||
hasPersistentNotifications() {
|
||||
@@ -31,15 +41,46 @@ class AlarmServiceDriverNode {
|
||||
throw new Error(`Trying to create a notification from an invalid object: ${JSON.stringify(notification)}`);
|
||||
}
|
||||
|
||||
const timeoutId = setTimeout(() => {
|
||||
const o = {
|
||||
appName: this.appName_,
|
||||
title: notification.title,
|
||||
};
|
||||
if ('body' in notification) o.message = notification.body;
|
||||
notifier.notify(o);
|
||||
this.clearNotification(notification.id);
|
||||
}, interval);
|
||||
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification ${notification.id} with interval: ${interval}ms`);
|
||||
|
||||
if (this.notifications_[notification.id]) clearTimeout(this.notifications_[notification.id].timeoutId);
|
||||
|
||||
let timeoutId = null;
|
||||
|
||||
// Note: setTimeout will break for values larger than Number.MAX_VALUE - in which case the timer
|
||||
// will fire immediately. So instead, if the interval is greater than a set max, reschedule after
|
||||
// that max interval.
|
||||
// https://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values/3468699
|
||||
|
||||
const maxInterval = 60 * 60 * 1000;
|
||||
if (interval >= maxInterval) {
|
||||
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification interval is greater than ${maxInterval}ms - will reschedule in ${maxInterval}ms`);
|
||||
|
||||
timeoutId = setTimeout(() => {
|
||||
if (!this.notifications_[notification.id]) {
|
||||
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification ${notification.id} has been deleted - not rescheduling it`);
|
||||
return;
|
||||
}
|
||||
this.scheduleNotification(this.notifications_[notification.id]);
|
||||
}, maxInterval);
|
||||
} else {
|
||||
timeoutId = setTimeout(() => {
|
||||
const o = {
|
||||
appID: this.appName_,
|
||||
title: notification.title,
|
||||
icon: `${bridge().electronApp().buildDir()}/icons/512x512.png`,
|
||||
};
|
||||
if ('body' in notification) o.message = notification.body;
|
||||
|
||||
this.logger().info('AlarmServiceDriverNode::scheduleNotification: Triggering notification:', o);
|
||||
|
||||
notifier.notify(o, (error, response) => {
|
||||
this.logger().info('AlarmServiceDriverNode::scheduleNotification: node-notifier response:', error, response);
|
||||
});
|
||||
|
||||
this.clearNotification(notification.id);
|
||||
}, interval);
|
||||
}
|
||||
|
||||
this.notifications_[notification.id] = Object.assign({}, notification);
|
||||
this.notifications_[notification.id].timeoutId = timeoutId;
|
||||
|
||||
Reference in New Issue
Block a user