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

Update addon-developer-support and use Services global variable if possible #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions src/chrome/content/helpers/notifyTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ var ADDON_ID = "";
* For usage descriptions, please check:
* https://github.com/thundernest/addon-developer-support/tree/master/scripts/notifyTools
*
* Version: 1.5
* Version 1.6
* - adjusted to Thunderbird Supernova (Services is now in globalThis)
*
* Version 1.5
* - deprecate enable(), disable() and registerListener()
* - add setAddOnId()
*
* Version: 1.4
* Version 1.4
* - auto enable/disable
*
* Version: 1.3
* Version 1.3
* - registered listeners for notifyExperiment can return a value
* - remove WindowListener from name of observer
*
Expand All @@ -26,7 +29,8 @@ var ADDON_ID = "";
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var Services = globalThis.Services ||
ChromeUtils.import("resource://gre/modules/Services.jsm").Services;

var notifyTools = {
registeredCallbacks: {},
Expand Down Expand Up @@ -161,4 +165,4 @@ if (typeof window != "undefined" && window) {
},
false
);
}
}
4 changes: 3 additions & 1 deletion src/chrome/content/hooks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* JS file to load XUL to display ThreadVis extension and include all scripts.
**********************************************************************************************************************/

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const Services = globalThis.Services || ChromeUtils.import(
"resource://gre/modules/Services.jsm"
).Services;

const { ThreadVis } = ChromeUtils.import("chrome://threadvis/content/threadvis.jsm");

Expand Down
4 changes: 3 additions & 1 deletion src/chrome/content/utils/preferences.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ var EXPORTED_SYMBOLS = [ "Preferences" ];
const { PreferenceKeys, PreferenceBranch } = ChromeUtils.import("chrome://threadvis/content/utils/preferenceskeys.jsm");

// Services
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const Services = globalThis.Services || ChromeUtils.import(
"resource://gre/modules/Services.jsm"
).Services;

const PREF_BOOL = Services.prefs.PREF_BOOL;
const PREF_INT = Services.prefs.PREF_INT;
Expand Down
4 changes: 3 additions & 1 deletion src/chrome/content/utils/strings.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

var EXPORTED_SYMBOLS = [ "Strings" ];

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const Services = globalThis.Services || ChromeUtils.import(
"resource://gre/modules/Services.jsm"
).Services;
const strings = Services.strings.createBundle("chrome://threadvis/locale/threadvis.properties");

/**
Expand Down
146 changes: 76 additions & 70 deletions src/experiments/NotifyTools/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
* This file is provided by the addon-developer-support repository at
* https://github.com/thundernest/addon-developer-support
*
* Version 1.5
* - adjusted to Thunderbird Supernova (Services is now in globalThis)
*
* Version 1.4
* - updated implementation to not assign this anymore
*
* Version 1.3
* - moved registering the observer into startup
*
Expand All @@ -17,58 +23,54 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

// Get various parts of the WebExtension framework that we need.
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
var { ExtensionSupport } = ChromeUtils.import("resource:///modules/ExtensionSupport.jsm");
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var NotifyTools = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
var self = this;

return {
NotifyTools: {

notifyExperiment(data) {
return new Promise(resolve => {
Services.obs.notifyObservers(
{ data, resolve },
"NotifyExperimentObserver",
self.extension.id
);
});
},

onNotifyBackground: new ExtensionCommon.EventManager({
context,
name: "NotifyTools.onNotifyBackground",
register: (fire) => {
let trackerId = self.observerTrackerNext++;
self.observerTracker[trackerId] = fire.sync;
return () => {
delete self.observerTracker[trackerId];
};
},
}).api(),
"use strict";

}
};
}
(function (exports) {

// Get various parts of the WebExtension framework that we need.
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
var Services = globalThis.Services ||
ChromeUtils.import("resource://gre/modules/Services.jsm").Services;

// Force API to run at startup, otherwise event listeners might not be added at the requested time. Also needs
// "events": ["startup"] in the experiment manifest
var observerTracker = new Set();

onStartup() {
let self = this;
class NotifyTools extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
return {
NotifyTools: {

this.observerTracker = {};
this.observerTrackerNext = 1;
notifyExperiment(data) {
return new Promise(resolve => {
Services.obs.notifyObservers(
{ data, resolve },
"NotifyExperimentObserver",
context.extension.id
);
});
},

onNotifyBackground: new ExtensionCommon.EventManager({
context,
name: "NotifyTools.onNotifyBackground",
register: (fire) => {
observerTracker.add(fire.sync);
return () => {
observerTracker.delete(fire.sync);
};
},
}).api(),

this.onNotifyBackgroundObserver = {
observe: async function (aSubject, aTopic, aData) {
}
};
}

// Force API to run at startup, otherwise event listeners might not be added at the requested time. Also needs
// "events": ["startup"] in the experiment manifest
onStartup() {
this.onNotifyBackgroundObserver = async (aSubject, aTopic, aData) => {
if (
Object.keys(self.observerTracker).length > 0 &&
aData == self.extension.id
observerTracker.size > 0 &&
aData == this.extension.id
) {
let payload = aSubject.wrappedJSObject;

Expand All @@ -77,7 +79,7 @@ var NotifyTools = class extends ExtensionCommon.ExtensionAPI {
if (payload.resolve) {
let observerTrackerPromises = [];
// Push listener into promise array, so they can run in parallel
for (let listener of Object.values(self.observerTracker)) {
for (let listener of observerTracker.values()) {
observerTrackerPromises.push(listener(payload.data));
}
// We still have to await all of them but wait time is just the time needed
Expand All @@ -101,33 +103,37 @@ var NotifyTools = class extends ExtensionCommon.ExtensionAPI {
} else {
// Older version of NotifyTools, which is not sending a resolve function, deprecated.
console.log("Please update the notifyTools API and the notifyTools script to at least v1.5");
for (let listener of Object.values(self.observerTracker)) {
for (let listener of observerTracker.values()) {
listener(payload.data);
}
}
}
},
};
// Add observer for notifyTools.js
Services.obs.addObserver(
this.onNotifyBackgroundObserver,
"NotifyBackgroundObserver",
false
);
}

onShutdown(isAppShutdown) {
if (isAppShutdown) {
return; // the application gets unloaded anyway
};

// Add observer for notifyTools.js
Services.obs.addObserver(
this.onNotifyBackgroundObserver,
"NotifyBackgroundObserver",
false
);
}

onShutdown(isAppShutdown) {
if (isAppShutdown) {
return; // the application gets unloaded anyway
}

// Remove observer for notifyTools.js
Services.obs.removeObserver(
this.onNotifyBackgroundObserver,
"NotifyBackgroundObserver"
);

// Flush all caches
Services.obs.notifyObservers(null, "startupcache-invalidate");
}
};

// Remove observer for notifyTools.js
Services.obs.removeObserver(
this.onNotifyBackgroundObserver,
"NotifyBackgroundObserver"
);
exports.NotifyTools = NotifyTools;

// Flush all caches
Services.obs.notifyObservers(null, "startupcache-invalidate");
}
};
})(this)
Loading