Popup is now calling the browser storage directly instead of using the messaging system. Should be a bit faster.

This commit is contained in:
Ryan Breen
2023-01-27 17:30:10 -05:00
parent b0faf6146e
commit 621e8b358b
2 changed files with 31 additions and 17 deletions

View File

@@ -1,4 +1,9 @@
import { bglog } from './utils'; import {
bglog,
getProfileNames,
setProfileIndex,
getProfileIndex,
} from './utils';
import Alpine from 'alpinejs'; import Alpine from 'alpinejs';
window.Alpine = Alpine; window.Alpine = Alpine;
@@ -13,40 +18,33 @@ Alpine.data('popup', () => ({
await browser.runtime.sendMessage({ kind: 'init' }); await browser.runtime.sendMessage({ kind: 'init' });
this.$watch('profileIndex', async () => { this.$watch('profileIndex', async () => {
await this.getProfileNames(); await this.loadNames();
await this.setProfileIndex(); await this.setProfileIndex();
}); });
// Even though getProfileIndex will immediately trigger a profile refresh, we still // Even though loadProfileIndex will immediately trigger a profile refresh, we still
// need to do an initial profile refresh first. This will pull the latest data from // need to do an initial profile refresh first. This will pull the latest data from
// the background scripts. Specifically, this pulls the list of profile names, // the background scripts. Specifically, this pulls the list of profile names,
// otherwise it generates a rendering error where it may not show the correct selected // otherwise it generates a rendering error where it may not show the correct selected
// profile when first loading the popup. // profile when first loading the popup.
await this.getProfileNames(); await this.loadNames();
await this.getProfileIndex(); await this.loadProfileIndex();
}, },
async setProfileIndex() { async setProfileIndex() {
// Becauset the popup state resets every time it open, we use null as a guard. That way // Becauset the popup state resets every time it open, we use null as a guard. That way
// whenever the user opens the popup, it doesn't automatically reset the current profile // whenever the user opens the popup, it doesn't automatically reset the current profile
if (this.profileIndex !== null) { if (this.profileIndex !== null) {
await browser.runtime.sendMessage({ await setProfileIndex(this.profileIndex);
kind: 'setProfileIndex',
payload: this.profileIndex,
});
} }
}, },
async getProfileNames() { async loadNames() {
this.profileNames = await browser.runtime.sendMessage({ this.profileNames = await getProfileNames();
kind: 'getProfileNames',
});
}, },
async getProfileIndex() { async loadProfileIndex() {
this.profileIndex = await browser.runtime.sendMessage({ this.profileIndex = await getProfileIndex();
kind: 'getProfileIndex',
});
}, },
async openOptions() { async openOptions() {

View File

@@ -1,6 +1,22 @@
const storage = browser.storage.local;
export async function bglog(msg, module = null) { export async function bglog(msg, module = null) {
await browser.runtime.sendMessage({ await browser.runtime.sendMessage({
kind: 'log', kind: 'log',
payload: { msg, module }, payload: { msg, module },
}); });
} }
export async function getProfileNames() {
let profiles = await storage.get({ profiles: [] });
return profiles.profiles.map(p => p.name);
}
export async function getProfileIndex() {
const index = await storage.get({ profileIndex: 0 });
return index.profileIndex;
}
export async function setProfileIndex(profileIndex) {
await storage.set({ profileIndex });
}