--- a/calendar/base/modules/calUtils.jsm
+++ b/calendar/base/modules/calUtils.jsm
@@ -13,16 +13,21 @@ Cc["@mozilla.org/calendar/backend-loader
// The calendar console instance
var gCalendarConsole = new ConsoleAPI({
prefix: "Lightning",
consoleID: "calendar",
maxLogLevel: Services.prefs.getBoolPref("calendar.debug.log", false) ? "all" : "warn",
});
+// Cache services to avoid calling getService over and over again. The cache is
+// a separate object to avoid polluting `cal`, and is defined here since a call
+// to `_service` will require it to already exist.
+var gServiceCache = {};
+
this.EXPORTED_SYMBOLS = ["cal"];
var cal = {
// These functions exist to reduce boilerplate code for creating instances
// as well as getting services and other (cached) objects.
createEvent: _instance("@mozilla.org/calendar/event;1", Ci.calIEvent, "icalString"),
createTodo: _instance("@mozilla.org/calendar/todo;1", Ci.calITodo, "icalString"),
createDateTime: _instance("@mozilla.org/calendar/datetime;1", Ci.calIDateTime, "icalString"),
createDuration: _instance("@mozilla.org/calendar/duration;1", Ci.calIDuration, "icalString"),
@@ -45,30 +50,27 @@ var cal = {
"icalString"
),
createRecurrenceInfo: _instance(
"@mozilla.org/calendar/recurrence-info;1",
Ci.calIRecurrenceInfo,
"item"
),
- getCalendarManager: _service("@mozilla.org/calendar/manager;1", Ci.calICalendarManager),
- getIcsService: _service("@mozilla.org/calendar/ics-service;1", Ci.calIICSService),
- getTimezoneService: _service("@mozilla.org/calendar/timezone-service;1", Ci.calITimezoneService),
+ getCalendarManager: _service("@mozilla.org/calendar/manager;1", "calICalendarManager"),
+ getIcsService: _service("@mozilla.org/calendar/ics-service;1", "calIICSService"),
+ getTimezoneService: _service("@mozilla.org/calendar/timezone-service;1", "calITimezoneService"),
getCalendarSearchService: _service(
"@mozilla.org/calendar/calendarsearch-service;1",
- Ci.calICalendarSearchProvider
+ "calICalendarSearchProvider"
),
- getFreeBusyService: _service("@mozilla.org/calendar/freebusy-service;1", Ci.calIFreeBusyService),
- getWeekInfoService: _service("@mozilla.org/calendar/weekinfo-service;1", Ci.calIWeekInfoService),
- getDateFormatter: _service(
- "@mozilla.org/calendar/datetime-formatter;1",
- Ci.calIDateTimeFormatter
- ),
- getDragService: _service("@mozilla.org/widget/dragservice;1", Ci.nsIDragService),
+ getFreeBusyService: _service("@mozilla.org/calendar/freebusy-service;1", "calIFreeBusyService"),
+ getWeekInfoService: _service("@mozilla.org/calendar/weekinfo-service;1", "calIWeekInfoService"),
+ getDateFormatter: _service("@mozilla.org/calendar/datetime-formatter;1", "calIDateTimeFormatter"),
+ getDragService: _service("@mozilla.org/widget/dragservice;1", "nsIDragService"),
/**
* The calendar console instance
*/
console: gCalendarConsole,
/**
* Logs a calendar message to the console. Needs calendar.debug.log enabled to show messages.
@@ -548,18 +550,20 @@ XPCOMUtils.defineLazyModuleGetter(
/**
* Returns a function that provides access to the given service.
*
* @param cid The contract id to create
* @param iid The interface id to create with
* @return {function} A function that returns the given service
*/
function _service(cid, iid) {
+ let name = `_${iid}`;
+ XPCOMUtils.defineLazyServiceGetter(gServiceCache, name, cid, iid);
return function() {
- return Cc[cid].getService(iid);
+ return gServiceCache[name];
};
}
/**
* Returns a function that creates an instance of the given component and
* optionally initializes it using the property name passed.
*
* @param cid The contract id to create