Skip to content

Commit

Permalink
Added command for switching to another connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelamud committed Mar 23, 2020
1 parent 5eb12cd commit 69fcdd9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';

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

const MAX_MATCHING_URLS_SIZE = 100;
let matchingUrls = new Map();
Expand Down Expand Up @@ -124,6 +124,9 @@ browser.runtime.onMessage.addListener(
case "deleteData":
deleteData(sender.tab.id, message.payload);
break;
case "switchData":
switchData(sender.tab.id, message.payload);
break;
}
}
);
24 changes: 24 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ export async function deleteData(tabId, location) {
broadcastMessage(data, clientUrl);
}
}

export async function switchData(tabId, location) {
const clientUrl = await getTabClientUrl(tabId);
const data = await dataLock.acquire("clientData", async () => {
const rootKey = `currentRoot;${clientUrl}`;
const rootsKey = `roots;${clientUrl}`;
let {[rootKey]: homeRoot, [rootsKey]: roots} = await browser.storage.local.get([rootKey, rootsKey]);
if (roots == null) {
roots = [];
}

if (!location || location === homeRoot || roots.find(r => r.url === location) == null) {
return loadedData();
}
await browser.storage.local.set({[rootKey]: location});

const dataKey = `clientData;${clientUrl};${location}`;
const {[dataKey]: clientData} = await browser.storage.local.get(dataKey);
return loadedData(location, clientData, roots);
});
if (data != null) {
broadcastMessage(data, clientUrl);
}
}

0 comments on commit 69fcdd9

Please sign in to comment.