Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Make clock/calendar aware of system timezone changes. Fixes #21
Browse files Browse the repository at this point in the history
Related to nodejs/node#4022
  • Loading branch information
mzdr committed Jul 17, 2017
1 parent acd1fe6 commit 6d04f01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/clock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Electron = require('electron'); // eslint-disable-line
const Moment = require('moment');
const ResetDateCache = require('reset-date-cache');

class Clock {
/**
Expand All @@ -16,6 +17,12 @@ class Clock {

// Start the clock
this.start();

// System timezone was changed during runtime…
Electron.systemPreferences.subscribeNotification(
'NSSystemTimeZoneDidChangeDistributedNotification',
() => this.onTimeZoneChange()
);
}

/**
Expand Down Expand Up @@ -53,9 +60,36 @@ class Clock {
* Sets the callback function which will be called on each tick.
*
* @param {function} callback Tick function to call.
* @return {Clock}
*/
onTick(callback) {
this.onTickHandler = callback;

return this;
}

/**
* System timezone was changed during runtime.
*
* @return {Clock}
*/
onTimeZoneChange() {
// Need to use a timeout since resetting the date cache right after the
// system event has occured didn’t work properly/consistently.
// Unfortunately I have no idea what’s the actual problem over here.
setTimeout(() => {
// Reset v8’s date cache…
// @see https://github.com/nodejs/node/issues/4022
// @see https://github.com/nodejs/node/issues/3449
ResetDateCache();

// Notifiy all webContents about the timezone change
(Electron.webContents.getAllWebContents() || []).forEach(
webContent => webContent.send('clock.timezone.changed')
);
}, 1000);

return this;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/components/calendar/calendar-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class CalendarView extends BaseElement {

document.addEventListener('keydown', e => this.onKeyDown(e));

// When the system timezone was changed…
Electron.ipcRenderer.on('clock.timezone.changed', () => this.update());

// Redraw calendar every minute to avoid displaying old/wrong states
setInterval(() => this.update(), 1000 * 60);

Expand Down

0 comments on commit 6d04f01

Please sign in to comment.