rust-search-extension/extension/index-manager.js

105 lines
3 KiB
JavaScript
Raw Normal View History

// Wrap chrome.storage API as a promise.
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/get
function getIndexInternal(key) {
return new Promise(resolve => {
chrome.storage.local.get(key, (result) => {
resolve(result[key]);
});
});
}
// Query all storage by this method:
// chrome.storage.local.get(null, function(result) {
// console.log('Value currently is ', result);
// });
2020-10-13 04:09:36 +00:00
class IndexManager {
static async getStdStableIndex() {
return await getIndexInternal('index-std-stable') || searchIndex;
2020-10-13 04:09:36 +00:00
}
static setStdStableIndex(index) {
chrome.storage.local.set({'index-std-stable': index});
2020-10-13 04:09:36 +00:00
}
static async getStdNightlyIndex() {
return await getIndexInternal('index-std-nightly') || searchIndex;
2020-10-13 04:09:36 +00:00
}
static setStdNightlyIndex(index) {
chrome.storage.local.set({'index-std-nightly': index});
2020-10-13 04:09:36 +00:00
}
static async getBookIndex() {
return await getIndexInternal('index-book') || booksIndex;
2020-10-13 04:09:36 +00:00
}
static setBookIndex(index) {
chrome.storage.local.set({'index-book': index});
2020-10-13 04:09:36 +00:00
}
static async getLabelIndex() {
return await getIndexInternal('index-label') || labelsIndex;
2020-10-13 04:09:36 +00:00
}
static setLabelIndex(index) {
chrome.storage.local.set({'index-label': index});
2020-10-13 04:09:36 +00:00
}
2021-10-16 04:14:40 +00:00
static async getRfcIndex() {
return await getIndexInternal('index-rfc') || rfcsIndex;
}
static setRfcIndex(index) {
chrome.storage.local.set({'index-rfc': index});
}
static async getCrateMapping() {
return await getIndexInternal('index-crate-mapping') || mapping;
2020-10-15 13:53:44 +00:00
}
static setCrateMapping(index) {
chrome.storage.local.set({'index-crate-mapping': index});
2020-10-15 13:53:44 +00:00
}
static async getCrateIndex() {
return await getIndexInternal('index-crate') || crateIndex;
2020-10-13 04:09:36 +00:00
}
static setCrateIndex(index) {
chrome.storage.local.set({'index-crate': index});
2020-10-13 04:09:36 +00:00
}
static async getLintIndex() {
return await getIndexInternal('index-lint') || lintsIndex;
2020-10-13 04:09:36 +00:00
}
static setLintIndex(index) {
chrome.storage.local.set({'index-lint': index});
2020-10-13 04:09:36 +00:00
}
static async getCaniuseIndex() {
return await getIndexInternal('index-caniuse') || caniuseIndex;
2020-10-13 04:09:36 +00:00
}
static setCaniuseIndex(index) {
chrome.storage.local.set({'index-caniuse': index});
2020-10-13 04:09:36 +00:00
}
static async getCommandIndex() {
2020-11-16 08:15:44 +00:00
let index = await getIndexInternal('index-command');
if (index) {
// commandsIndex would update if the new version installed.
// So we should override the old cache one.
if (Object.keys(index).length < Object.keys(commandsIndex).length) {
this.setCommandIndex(commandsIndex);
return commandsIndex;
}
return index;
}
return commandsIndex;
2020-10-23 07:46:55 +00:00
}
static setCommandIndex(index) {
chrome.storage.local.set({'index-command': index});
2020-10-23 07:46:55 +00:00
}
2020-10-13 04:09:36 +00:00
}