Skip to content

Commit

Permalink
Report node name (from roots array) in the client data.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelamud committed Mar 23, 2020
1 parent 79d04f1 commit f56043c
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ function loadedData(homeRoot, clientData, roots) {
};
}

function getRootName(roots, location) {
const root = roots.find(r => r.url === location);
return root ? root.name : null;
}

function setRoot(roots, location, nodeName) {
if (roots.find(r => r.url === location) == null) {
roots.push({url: location, name: nodeName});
} else {
roots = roots.map(r => r.url === location ? {url: location, name: nodeName} : r);
}
return roots;
}

export async function loadData(tabId) {
const clientUrl = await getTabClientUrl(tabId);
const rootKey = `currentRoot;${clientUrl}`;
Expand All @@ -97,6 +111,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);
}

Expand All @@ -116,11 +131,7 @@ export async function storeData(tabId, data) {
await browser.storage.local.set({[rootKey]: location});
homeRoot = location;
}
if (roots.find(r => r.url === homeRoot) == null) {
roots.push({url: homeRoot, name: nodeName});
} else {
roots = roots.map(r => r.url === homeRoot ? {url: homeRoot, name: nodeName} : r);
}
roots = setRoot(roots, homeRoot, nodeName);
await browser.storage.local.set({[rootsKey]: roots});
}
if (!homeRoot) {
Expand Down Expand Up @@ -167,10 +178,12 @@ export async function deleteData(tabId, location) {
return loadedData(homeRoot, {}, roots);
}
homeRoot = roots[roots.length - 1].url;
const nodeName = roots[roots.length - 1].name;
await browser.storage.local.set({[rootKey]: homeRoot});

const dataKey = `clientData;${clientUrl};${homeRoot}`;
const {[dataKey]: clientData} = await browser.storage.local.get(dataKey);
ObjectPath.set(clientData, "home.nodeName", nodeName);
return loadedData(homeRoot, clientData, roots);
}
return loadedData(homeRoot, {}, roots);
Expand All @@ -190,13 +203,15 @@ export async function switchData(tabId, location) {
roots = [];
}

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

const dataKey = `clientData;${clientUrl};${location}`;
const {[dataKey]: clientData} = await browser.storage.local.get(dataKey);
ObjectPath.set(clientData, "home.nodeName", root.name);
return loadedData(location, clientData, roots);
});
if (data != null) {
Expand Down

0 comments on commit f56043c

Please sign in to comment.