Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify lazy initialisation of TimezoneService. #654

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading