Skip to content

Commit

Permalink
Move tab-related and settings-related functions into separate modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelamud committed Mar 24, 2020
1 parent cdf4781 commit 7609ece
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import browser from 'webextension-polyfill';

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

const MAX_MATCHING_URLS_SIZE = 100;
let matchingUrls = new Map();
Expand Down
2 changes: 1 addition & 1 deletion src/content.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import browser from 'webextension-polyfill';
import * as Base64js from 'base64-js';

import { getClientUrl } from "./data";
import { getClientUrl } from "./settings";

let scriptCode = '(' + function() {
fetch("%URL%", {redirect: "follow"})
Expand Down
40 changes: 2 additions & 38 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import browser from 'webextension-polyfill';
import AsyncLock from 'async-lock';
import ObjectPath from 'object-path';

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

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

const activeTabs = new Map();
const dataLock = new AsyncLock();

export async function isStorageV1() {
Expand All @@ -31,43 +32,6 @@ export async function migrateStorageToV2() {
await browser.storage.local.set(data);
}

export async function getSettings() {
let {defaultClient, customClientUrl} = await browser.storage.local.get(["defaultClient", "customClientUrl"]);
defaultClient = defaultClient != null ? defaultClient : true;
customClientUrl = customClientUrl ? customClientUrl : DEFAULT_CLIENT_URL;
return {defaultClient, customClientUrl};
}

export async function setSettings({defaultClient, customClientUrl}) {
await browser.storage.local.set({defaultClient, customClientUrl});
}

export async function getClientUrl() {
const {defaultClient, customClientUrl} = await browser.storage.local.get(["defaultClient", "customClientUrl"]);
return defaultClient == null || defaultClient ? DEFAULT_CLIENT_URL : customClientUrl;
}

export async function addTab(tabId) {
const clientUrl = await getClientUrl();
activeTabs.set(tabId, {clientUrl});
}

function broadcastMessage(message, clientUrl) {
let closedTabs = [];
activeTabs.forEach((value, tabId) => {
if (value.clientUrl === clientUrl) {
browser.tabs.sendMessage(tabId, message)
.catch(() => closedTabs.push(tabId));
}
});
closedTabs.forEach(tabId => activeTabs.delete(tabId));
}

async function getTabClientUrl(tabId) {
const {clientUrl} = activeTabs.get(tabId);
return clientUrl != null ? clientUrl : await getClientUrl();
}

function loadedData(homeRoot, clientData, roots) {
let data = {};
if (homeRoot) {
Expand Down
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSettings, setSettings } from "./data";
import { getSettings, setSettings } from "./settings";

function isDefaultClient() {
return document.querySelector("#default-client").checked;
Expand Down
17 changes: 17 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import browser from "webextension-polyfill";

export async function getSettings() {
let {defaultClient, customClientUrl} = await browser.storage.local.get(["defaultClient", "customClientUrl"]);
defaultClient = defaultClient != null ? defaultClient : true;
customClientUrl = customClientUrl ? customClientUrl : DEFAULT_CLIENT_URL;
return {defaultClient, customClientUrl};
}

export async function setSettings({defaultClient, customClientUrl}) {
await browser.storage.local.set({defaultClient, customClientUrl});
}

export async function getClientUrl() {
const {defaultClient, customClientUrl} = await browser.storage.local.get(["defaultClient", "customClientUrl"]);
return defaultClient == null || defaultClient ? DEFAULT_CLIENT_URL : customClientUrl;
}
26 changes: 26 additions & 0 deletions src/tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import browser from 'webextension-polyfill';

import { getClientUrl } from "./settings";

const activeTabs = new Map();

export async function addTab(tabId) {
const clientUrl = await getClientUrl();
activeTabs.set(tabId, {clientUrl});
}

export function broadcastMessage(message, clientUrl) {
let closedTabs = [];
activeTabs.forEach((value, tabId) => {
if (value.clientUrl === clientUrl) {
browser.tabs.sendMessage(tabId, message)
.catch(() => closedTabs.push(tabId));
}
});
closedTabs.forEach(tabId => activeTabs.delete(tabId));
}

export async function getTabClientUrl(tabId) {
const {clientUrl} = activeTabs.get(tabId);
return clientUrl != null ? clientUrl : await getClientUrl();
}

0 comments on commit 7609ece

Please sign in to comment.