1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00
Files
.github
Assets
CliClient
Clipper
ElectronClient
ReactNativeClient
android
ios
lib
components
images
joplin-renderer
migrations
models
renderers
services
rest
AlarmService.js
AlarmServiceDriver.android.js
AlarmServiceDriver.ios.js
AlarmServiceDriverNode.js
BaseService.js
DecryptionWorker.js
EncryptionService.js
EncryptionServiceDriverNode.js
ExternalEditWatcher.js
InteropService.js
InteropService_Exporter_Base.js
InteropService_Exporter_Html.js
InteropService_Exporter_Jex.js
InteropService_Exporter_Json.js
InteropService_Exporter_Md.js
InteropService_Exporter_Raw.js
InteropService_Importer_Base.js
InteropService_Importer_EnexToHtml.js
InteropService_Importer_EnexToMd.js
InteropService_Importer_Jex.js
InteropService_Importer_Md.js
InteropService_Importer_Raw.js
ItemChangeUtils.js
KvStore.js
MigrationService.js
ModelCache.js
NavService.js
PluginManager.js
ResourceFetcher.js
ResourceService.js
RevisionService.js
SearchEngine.js
SearchEngineUtils.js
back-button.js
report.js
vendor
ArrayUtils.js
BaseApplication.js
BaseModel.js
BaseSyncTarget.js
Cache.js
ClipperServer.js
CssUtils.js
DropboxApi.js
EventDispatcher.js
HtmlToMd.js
JoplinError.js
JoplinServerApi.ts
ModelCache.js
ObjectUtils.js
SyncTargetDropbox.js
SyncTargetFilesystem.js
SyncTargetMemory.js
SyncTargetNextcloud.js
SyncTargetOneDrive.js
SyncTargetOneDriveDev.js
SyncTargetRegistry.js
SyncTargetWebDAV.js
TaskQueue.js
TemplateUtils.js
WebDavApi.js
WelcomeUtils.js
database-driver-node.js
database-driver-react-native.js
database.js
dialogs.js
file-api-driver-dropbox.js
file-api-driver-local.js
file-api-driver-memory.js
file-api-driver-onedrive.js
file-api-driver-webdav.js
file-api.js
folders-screen-utils.js
fs-driver-base.js
fs-driver-dummy.js
fs-driver-node.js
fs-driver-rn.js
geolocation-node.js
geolocation-react.js
htmlUtils.js
import-enex-html-gen.js
import-enex-md-gen.js
import-enex.js
joplin-database.js
locale.js
logger.js
markJsUtils.js
markdownUtils.js
markupLanguageUtils.js
mime-utils.js
net-utils.js
onedrive-api-node-utils.js
onedrive-api.js
package.json
parameters.js
parseUri.js
path-utils.js
poor-man-intervals.js
promise-utils.js
randomClipperPort.js
react-logger.js
reducer.js
registry.js
resourceUtils.js
shim-init-node.js
shim-init-react.js
shim.js
string-utils-common.js
string-utils.js
synchronizer.js
time-utils.js
urlUtils.js
uuid.js
welcomeAssets.js
locales
.buckconfig
.flowconfig
.gitattributes
.gitignore
.watchmanconfig
PluginAssetsLoader.ts
app.json
build_android.bat
build_android_prod.bat
clean_build.bat
debug_log.bat
debug_log.sh
encodeAssets.js
index.android.js
index.ios.js
index.js
main.js
metro.config.js
package-lock.json
package.json
root.js
run_ios.sh
start_server.bat
start_server.sh
Tools
docs
readme
.eslintignore
.eslintrc.js
.gitignore
.travis.yml
BUILD.md
CONTRIBUTING.md
Joplin_install_and_update.sh
LICENSE
README.md
SECURITY.md
_config.yml
appveyor.yml
joplin.code-workspace
joplin.sublime-project
linkToLocal.sh
package-lock.json
package.json
tsconfig.json
joplin/ReactNativeClient/lib/services/AlarmServiceDriverNode.js

96 lines
3.2 KiB
JavaScript
Raw Normal View History

const notifier = require('node-notifier');
const { bridge } = require('electron').remote.require('./bridge');
class AlarmServiceDriverNode {
constructor(options) {
// Note: appName is required to get the notification to work. It must be the same as the appId defined in package.json
// 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() {
return false;
}
notificationIsSet(id) {
return id in this.notifications_;
}
async clearNotification(id) {
if (!this.notificationIsSet(id)) return;
clearTimeout(this.notifications_[id].timeoutId);
delete this.notifications_[id];
}
2019-07-29 15:43:53 +02:00
async scheduleNotification(notification) {
const now = Date.now();
const interval = notification.date.getTime() - now;
if (interval < 0) return;
if (isNaN(interval)) {
2019-09-19 22:51:18 +01:00
throw new Error(`Trying to create a notification from an invalid object: ${JSON.stringify(notification)}`);
}
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;
// Message is required on Windows 7 however we don't want to repeat the title so
// make it an empty string.
// https://github.com/laurent22/joplin/issues/2144
if (!o.message) o.message = '-';
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;
}
}
2019-07-29 15:43:53 +02:00
module.exports = AlarmServiceDriverNode;