From 3cc84971858e2af3893ca637b863308f42564c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20R=C3=A8gne?= Date: Wed, 11 Aug 2021 11:25:15 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20G=C3=A9rer=20l'acc=C3=A8s=20asynchrone?= =?UTF-8?q?=20=C3=A0=20la=20configuration.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background/index.html | 4 +++- src/background/menu.js | 4 ++-- src/background/migrate.js | 14 ++++++-------- src/core/notify.js | 8 +++++--- test/unit/core/notify.js | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/background/index.html b/src/background/index.html index 687b28b3..8cc923c4 100644 --- a/src/background/index.html +++ b/src/background/index.html @@ -6,8 +6,10 @@ - + + diff --git a/src/background/menu.js b/src/background/menu.js index a3fc630c..79cd39c6 100644 --- a/src/background/menu.js +++ b/src/background/menu.js @@ -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), }); } diff --git a/src/background/migrate.js b/src/background/migrate.js index 3666a149..4560a517 100644 --- a/src/background/migrate.js +++ b/src/background/migrate.js @@ -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"]) { @@ -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", @@ -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: "" }], diff --git a/src/core/notify.js b/src/core/notify.js index dffa4285..18d387fd 100644 --- a/src/core/notify.js +++ b/src/core/notify.js @@ -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} 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"), diff --git a/test/unit/core/notify.js b/test/unit/core/notify.js index 52e59536..6964bba2 100644 --- a/test/unit/core/notify.js +++ b/test/unit/core/notify.js @@ -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", }]); @@ -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.", }]);