Skip to content

Commit

Permalink
fix: Gérer l'accès asynchrone à la configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Aug 13, 2021
1 parent 63962a3 commit 3cc8497
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/background/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
</head>
<body>
<script src="/polyfill/script.js" type="module"></script>
<script src="migrate.js" type="module"></script>
<!-- Charger le menu avec l'écouteur browser.storage.onChanged avant que la
configuration soit modifiée dans la migration. -->
<script src="menu.js" type="module"></script>
<script src="migrate.js" type="module"></script>
<script src="permissions.js" type="module"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions src/background/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const handleClick = async function (info) {
const urls = await aggregate(info);
await cast(info.menuItemId, urls);
} catch (err) {
notify(err);
await notify(err);
}
} else if (!info.wasChecked) {
browser.storage.local.set({
await browser.storage.local.set({
"server-active": Number.parseInt(info.menuItemId, 10),
});
}
Expand Down
14 changes: 6 additions & 8 deletions src/background/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* @module
*/

browser.storage.local.get().then((current) => {
browser.storage.local.get().then(async (current) => {
// Vider la configuration pour enlever les éventuelles propriétés obsolètes.
await browser.storage.local.clear();

if ("config-version" in current) {
let config = current;
if (1 === config["config-version"]) {
Expand All @@ -14,10 +17,6 @@ browser.storage.local.get().then((current) => {
.filter(([k, v]) => k.startsWith("contexts-") && v)
.map(([k]) => k.slice(9));

// Nettoyer la configuration pour garder seulement les propriétés
// nécessaire.
browser.storage.local.clear();

config = {
"config-version": 2,
"server-mode": "single",
Expand Down Expand Up @@ -46,10 +45,9 @@ browser.storage.local.get().then((current) => {
config["youtube-order"] = "";
}

browser.storage.local.set(config);
await browser.storage.local.set(config);
} else {
browser.storage.local.clear();
browser.storage.local.set({
await browser.storage.local.set({
"config-version": 4,
"server-mode": "single",
"server-list": [{ address: "", name: "" }],
Expand Down
8 changes: 5 additions & 3 deletions src/core/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import { PebkacError } from "./pebkac.js";
* Notifie l'utilisateur d'un message d'erreur.
*
* @param {PebkacError|Error} err L'erreur affichée dans la notification.
* @returns {Promise<string>} Une promesse contenant l'identifiant de la
* notification.
*/
export const notify = function (err) {
// Ne pas ajouter un bouton vers la configuration car cette fonctionnalité
// n'est pas encore implémentée.
// n'est pas encore implémentée dans Firefox.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1190681
browser.notifications.create({
return browser.notifications.create({
type: "basic",
iconUrl: "img/icon.svg",
iconUrl: "/img/icon.svg",
title: err instanceof PebkacError
? err.title
: browser.i18n.getMessage("notifications_unknown_title"),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/core/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("core/notify.js", function () {
assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, [{
type: "basic",
iconUrl: "img/icon.svg",
iconUrl: "/img/icon.svg",
title: "Unknown error",
message: "foo",
}]);
Expand All @@ -29,7 +29,7 @@ describe("core/notify.js", function () {
assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, [{
type: "basic",
iconUrl: "img/icon.svg",
iconUrl: "/img/icon.svg",
title: "Unsupported link",
message: "Link foo is invalid.",
}]);
Expand Down

0 comments on commit 3cc8497

Please sign in to comment.