Make action name more explicit

This commit is contained in:
Folyd 2020-10-14 21:27:38 +08:00
parent 6216ecb9bd
commit fe6e662dad
2 changed files with 8 additions and 7 deletions

View file

@ -266,7 +266,7 @@ chrome.runtime.setUninstallURL(
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) { switch (message.action) {
// Stable:* action is exclusive to nightly docs event // Stable:* action is exclusive to stable docs event
case "stable:add" : { case "stable:add" : {
IndexManager.setStdStableIndex(message.searchIndex); IndexManager.setStdStableIndex(message.searchIndex);
// New stdSearcher instance after docs updated // New stdSearcher instance after docs updated
@ -282,18 +282,19 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
sendResponse(true); sendResponse(true);
break; break;
} }
case "check": { // Crate:* action is exclusive to crate event
case "crate:check": {
let crates = CrateDocSearchManager.getCrates(); let crates = CrateDocSearchManager.getCrates();
sendResponse(crates[message.crateName]); sendResponse(crates[message.crateName]);
break; break;
} }
case "add": { case "crate:add": {
CrateDocSearchManager.addCrate(message.crateName, message.crateVersion, message.searchIndex); CrateDocSearchManager.addCrate(message.crateName, message.crateVersion, message.searchIndex);
crateDocSearchManager.initAllCrateSearcher(); crateDocSearchManager.initAllCrateSearcher();
sendResponse(true); sendResponse(true);
break; break;
} }
case "remove": { case "crate:remove": {
CrateDocSearchManager.removeCrate(message.crateName); CrateDocSearchManager.removeCrate(message.crateName);
crateDocSearchManager.initAllCrateSearcher(); crateDocSearchManager.initAllCrateSearcher();
sendResponse(true); sendResponse(true);

View file

@ -8,7 +8,7 @@ document.addEventListener("DOMContentLoaded", async () => {
let childrenNumber = ul.children.length; let childrenNumber = ul.children.length;
if (childrenNumber >= 3) { if (childrenNumber >= 3) {
await insertFeatureFlagsElement(childrenNumber); await insertFeatureFlagsElement(childrenNumber);
chrome.runtime.sendMessage({crateName, action: "check"}, crate => { chrome.runtime.sendMessage({crateName, action: "crate:check"}, crate => {
if (crate) { if (crate) {
currentCrateVersion = crate.version; currentCrateVersion = crate.version;
} }
@ -67,7 +67,7 @@ function insertAddToExtensionElement() {
li.onclick = () => { li.onclick = () => {
// Toggle search index added state // Toggle search index added state
if (state === "latest") { if (state === "latest") {
chrome.runtime.sendMessage({crateName, action: "remove"}, response => { chrome.runtime.sendMessage({crateName, action: "crate:remove"}, response => {
currentCrateVersion = undefined; currentCrateVersion = undefined;
insertAddToExtensionElement(); insertAddToExtensionElement();
}); });
@ -106,7 +106,7 @@ window.addEventListener("message", function(event) {
if (event.source === window && if (event.source === window &&
event.data && event.data &&
event.data.direction === "rust-search-extension") { event.data.direction === "rust-search-extension") {
chrome.runtime.sendMessage({action: "add", ...event.data.message}, chrome.runtime.sendMessage({action: "crate:add", ...event.data.message},
(response) => { (response) => {
console.log(response); console.log(response);
} }