Skip to content

Commit

Permalink
Simplify lazy initialisation of TimezoneService. (#654)
Browse files Browse the repository at this point in the history
It appears the current way of bootstrapping the core zones exists because TimezoneService's position among the modules means we can't call reset immediately.

Something seem to not work right and leads to Time.epochTime being in the floating time zone instead of UTC. At least when run in Thunderbird, I'm not sure if it always happens.
My patch just creates the zones on first use.
  • Loading branch information
darktrojan committed Apr 9, 2024
1 parent 59245d9 commit b7e42f7
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions lib/ical/timezone_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,7 @@
import Timezone from "./timezone.js";
import Component from "./component.js";

let zones = {
};

function lazyZone(name) {
return {
configurable: true,
enumerable: true,
get: function() {
delete this[name];
TimezoneService.reset();
return this[name];
}
};
}

Object.defineProperties(zones, {
Z: lazyZone("Z"),
UTC: lazyZone("UTC"),
GMT: lazyZone("GMT"),
});
let zones = null;

/**
* @classdesc
Expand All @@ -38,6 +19,10 @@ Object.defineProperties(zones, {
*/
const TimezoneService = {
get count() {
if (zones === null) {
return 0;
}

return Object.keys(zones).length;
},

Expand All @@ -57,6 +42,10 @@ const TimezoneService = {
* @return {Boolean} False, when not present
*/
has: function(tzid) {
if (zones === null) {
return false;
}

return !!zones[tzid];
},

Expand All @@ -67,6 +56,10 @@ const TimezoneService = {
* @return {?ICAL.Timezone} The timezone, or null if not found
*/
get: function(tzid) {
if (zones === null) {
this.reset();
}

return zones[tzid];
},

Expand All @@ -80,6 +73,10 @@ const TimezoneService = {
* The initialized zone or vtimezone.
*/
register: function(name, timezone) {
if (zones === null) {
this.reset();
}

if (name instanceof Component) {
if (name.name === 'vtimezone') {
timezone = new Timezone(name);
Expand All @@ -101,6 +98,10 @@ const TimezoneService = {
* @return {?ICAL.Timezone} The removed timezone, or null if not registered
*/
remove: function(tzid) {
if (zones === null) {
return null;
}

return (delete zones[tzid]);
}
};
Expand Down

0 comments on commit b7e42f7

Please sign in to comment.