Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat: Gérer un état 'stopped' local en plus de l'état synchronisé
Browse files Browse the repository at this point in the history
  • Loading branch information
julienw committed May 27, 2021
1 parent d7be6bb commit 8d1be38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 19 additions & 7 deletions background_scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
WORKING: "w",
};

async function updateIconStatus() {
async function updateIconStatus(stopped) {
return await browser.browserAction.setIcon({
path: {
16: `../icons/vaccine-${stopped ? "black" : "color"}.svg`,
Expand Down Expand Up @@ -57,7 +57,7 @@
}

async function executeNextJob() {
const { stopped } = await browser.storage.sync.get({
const { stopped } = await browser.storage.local.get({
stopped: false,
});

Expand Down Expand Up @@ -93,9 +93,7 @@
}

browser.storage.onChanged.addListener(async (change, areaName) => {
if (areaName !== "sync") return;

if (change.locations && change.locations.newValue) {
if (areaName === "sync" && change.locations && change.locations.newValue) {
Object.keys(locations).forEach((url) => {
if (!change.locations.newValue[url]) {
delete locations[url];
Expand All @@ -111,7 +109,14 @@
});
}

if (change.stopped) return await updateIconStatus(stopped);
if (change.stopped) {
await updateIconStatus(change.stopped.newValue);
if (areaName === "sync") {
// Ça peut arriver de cette instance ou d'une autre instance de Firefox
// -> mettons aussi à jour la valeur locale pour arrêter les checks locaux.
await browser.storage.local.set({ stopped: true });
}
}
});

browser.runtime.onMessage.addListener(async (data) => {
Expand Down Expand Up @@ -144,6 +149,7 @@
break;

case "booked":
// Note: on met à jour la valeur locale dans onChanged au-dessus.
await browser.storage.sync.set({ stopped: true });

await browser.tabs.create({
Expand All @@ -170,11 +176,17 @@
}
});

const { locations, stopped } = await browser.storage.sync.get({
// Le booléan "stopped" est à la fois stocké localement et synchronisé. En
// effet, lorsqu'on arrive à booker un rdv dans un Firefox on veut arrêter les
// checks dans toutes les instances. Mais un simple clic sur le bouton ne doit
// déclencher d'arrêt que localement.
const { locations, stopped: stoppedFromSync } = await browser.storage.sync.get({
locations: {},
stopped: false,
});

const { stopped } = await browser.storage.local.get({ stopped: stoppedFromSync });

await updateIconStatus(stopped);

// On ajoute les iframes pour charger les centres à surveiller en arrière plan
Expand Down
16 changes: 8 additions & 8 deletions browser_action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@
document.getElementById(stopped ? "stop" : "start").style = "display: none";
}

let { locations, stopped, autoBook } = await browser.storage.sync.get({
let { locations, autoBook } = await browser.storage.sync.get({
locations: {},
autoBook: false,
stopped: false,
});

let { stopped } = await browser.storage.local.get({ stopped: false });

let { localLocations } = await browser.storage.local.get({ locations: {} });

browser.storage.onChanged.addListener(async (change, areaName) => {
Expand All @@ -79,6 +80,8 @@
localLocations = change.locations.newValue;
displayLocations(locations, localLocations);
}

if (change.stopped) displayStopStart(change.stopped.newValue || false);
}

if (areaName === "sync") {
Expand All @@ -87,8 +90,6 @@
displayLocations(locations, localLocations);
}

if (change.stopped) displayStopStart(change.stopped.newValue || false);

if (change.autoBook)
document.getElementById(
change.autoBook.newValue || false
Expand All @@ -99,13 +100,11 @@
});

document.getElementById("stop").onclick = async () => {
await browser.storage.sync.set({ stopped: true });
displayStopStart(true);
await browser.storage.local.set({ stopped: true });
};

document.getElementById("start").onclick = async () => {
await browser.storage.sync.set({ stopped: false });
displayStopStart(false);
await browser.storage.local.set({ stopped: false });
};

document.getElementById("reset").onclick = async () => {
Expand All @@ -117,6 +116,7 @@
return;

await browser.storage.sync.clear();
await browser.storage.local.clear();
};

document.getElementById("disableAutoBook").onclick = async () =>
Expand Down

0 comments on commit 8d1be38

Please sign in to comment.