Skip to content

Commit

Permalink
Commands for caching names.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelamud committed Mar 24, 2020
1 parent 7609ece commit 31b8c53
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import browser from 'webextension-polyfill';

import { deleteData, isStorageV1, loadData, migrateStorageToV2, storeData, switchData } from "./data";
import { addTab } from "./tabs";
import { storeName } from "./names";

const MAX_MATCHING_URLS_SIZE = 100;
let matchingUrls = new Map();
Expand Down Expand Up @@ -128,6 +129,9 @@ browser.runtime.onMessage.addListener(
case "switchData":
switchData(sender.tab.id, message.payload);
break;
case "storeName":
storeName(sender.tab.id, message.payload);
break;
}
}
);
10 changes: 8 additions & 2 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AsyncLock from 'async-lock';
import ObjectPath from 'object-path';

import { broadcastMessage, getTabClientUrl } from "./tabs";
import { clearNames, getNames } from "./names";

const DEFAULT_CLIENT_URL = "https://client.moera.org/releases/latest";

Expand Down Expand Up @@ -32,7 +33,7 @@ export async function migrateStorageToV2() {
await browser.storage.local.set(data);
}

function loadedData(homeRoot, clientData, roots) {
function loadedData(homeRoot, clientData, roots, names) {
let data = {};
if (homeRoot) {
data = {...clientData};
Expand All @@ -41,6 +42,9 @@ function loadedData(homeRoot, clientData, roots) {
if (roots != null) {
data = {...data, roots}
}
if (names != null) {
data = {...data, names}
}
return {
source: "moera",
action: "loadedData",
Expand Down Expand Up @@ -76,7 +80,7 @@ export async function loadData(tabId) {
const dataKey = `clientData;${clientUrl};${homeRoot}`;
const {[dataKey]: clientData} = await browser.storage.local.get(dataKey);
ObjectPath.set(clientData, "home.nodeName", getRootName(roots, homeRoot));
return loadedData(homeRoot, clientData, roots);
return loadedData(homeRoot, clientData, roots, await getNames(clientUrl));
}

export async function storeData(tabId, data) {
Expand Down Expand Up @@ -138,6 +142,7 @@ export async function deleteData(tabId, location) {

let nodeName;
if (location === homeRoot) {
await clearNames(clientUrl);
if (roots.length === 0) {
await browser.storage.local.remove(rootKey);
return loadedData(homeRoot, {}, roots);
Expand Down Expand Up @@ -173,6 +178,7 @@ export async function switchData(tabId, location) {
return loadedData();
}
await browser.storage.local.set({[rootKey]: location});
await clearNames(clientUrl);

const dataKey = `clientData;${clientUrl};${location}`;
const {[dataKey]: clientData} = await browser.storage.local.get(dataKey);
Expand Down
49 changes: 49 additions & 0 deletions src/names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import browser from 'webextension-polyfill';
import AsyncLock from 'async-lock';

import { broadcastMessage, getTabClientUrl } from "./tabs";

const NAME_TTL = 6 * 60 * 60; // seconds
const MAX_NAMES_SIZE = 500;
const namesLock = new AsyncLock();

function storedName(details) {
return {
source: "moera",
action: "storedName",
payload: details
};
}

export async function getNames(clientUrl) {
const namesKey = `names;${clientUrl}`;
const {[namesKey]: names} = await browser.storage.local.get(namesKey);
return names != null ? names : [];
}

export async function clearNames(clientUrl) {
await namesLock.acquire("names", async () => {
const namesKey = `names;${clientUrl}`;
await browser.storage.local.remove(namesKey);
});
}

export async function storeName(tabId, details) {
const clientUrl = await getTabClientUrl(tabId);
await namesLock.acquire("names", async () => {
const namesKey = `names;${clientUrl}`;
let {[namesKey]: names} = await browser.storage.local.get(namesKey);
if (names == null) {
names = [];
}
const now = Math.round(Date.now() / 1000);
names = names.filter(info => info.name !== details.name && (now - info.updated) <= NAME_TTL);
details.updated = now;
names.push(details);
if (names.length > MAX_NAMES_SIZE) {
names.splice(0, names.length - MAX_NAMES_SIZE);
}
await browser.storage.local.set({[namesKey]: names});
});
broadcastMessage(storedName(details), clientUrl);
}

0 comments on commit 31b8c53

Please sign in to comment.