From 47787c0b0b846d5f4a057661efdd05d8786032f1 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 6 Apr 2020 23:32:01 +0530 Subject: make linter less grumpy --- src/webex/wxBackend.ts | 89 +++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 40 deletions(-) (limited to 'src/webex/wxBackend.ts') diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index ef4715dce..f26c14d37 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -40,7 +40,6 @@ import { BrowserHttpLib } from "../util/http"; import { OpenedPromise, openPromise } from "../util/promiseUtils"; import { classifyTalerUri, TalerUriType } from "../util/taleruri"; import { Wallet } from "../wallet"; -import { ChromeBadge } from "./chromeBadge"; import { isFirefox } from "./compat"; import { MessageType } from "./messages"; import * as wxApi from "./wxApi"; @@ -49,6 +48,21 @@ import { Database } from "../util/query"; const NeedsWallet = Symbol("NeedsWallet"); +/** + * Currently active wallet instance. Might be unloaded and + * re-instantiated when the database is reset. + */ +let currentWallet: Wallet | undefined; + +let currentDatabase: IDBDatabase | undefined; + +/** + * Last version if an outdated DB, if applicable. + */ +let outdatedDbVersion: number | undefined; + +const walletInit: OpenedPromise = openPromise(); + async function handleMessage( sender: MessageSender, type: MessageType, @@ -57,14 +71,12 @@ async function handleMessage( function assertNotFound(t: never): never { console.error(`Request type ${t as string} unknown`); console.error(`Request detail was ${detail}`); - return ( - { - error: { - message: `request type ${t as string} unknown`, - requestType: type, - }, - } as never - ); + return { + error: { + message: `request type ${t as string} unknown`, + requestType: type, + }, + } as never; } function needsWallet(): Wallet { if (!currentWallet) { @@ -320,7 +332,7 @@ function getTab(tabId: number): Promise { }); } -function setBadgeText(options: chrome.browserAction.BadgeTextDetails) { +function setBadgeText(options: chrome.browserAction.BadgeTextDetails): void { // not supported by all browsers ... if (chrome && chrome.browserAction && chrome.browserAction.setBadgeText) { chrome.browserAction.setBadgeText(options); @@ -331,9 +343,12 @@ function setBadgeText(options: chrome.browserAction.BadgeTextDetails) { function waitMs(timeoutMs: number): Promise { return new Promise((resolve, reject) => { - chrome.extension - .getBackgroundPage()! - .setTimeout(() => resolve(), timeoutMs); + const bgPage = chrome.extension.getBackgroundPage(); + if (!bgPage) { + reject("fatal: no background page"); + return; + } + bgPage.setTimeout(() => resolve(), timeoutMs); }); } @@ -359,7 +374,7 @@ function makeSyncWalletRedirect( if (isFirefox()) { // Some platforms don't support the sync redirect (yet), so fall back to // async redirect after a timeout. - const doit = async () => { + const doit = async (): Promise => { await waitMs(150); const tab = await getTab(tabId); if (tab.url === oldUrl) { @@ -371,29 +386,13 @@ function makeSyncWalletRedirect( return { redirectUrl: outerUrl.href }; } -/** - * Currently active wallet instance. Might be unloaded and - * re-instantiated when the database is reset. - */ -let currentWallet: Wallet | undefined; - -let currentDatabase: IDBDatabase | undefined; - -/** - * Last version if an outdated DB, if applicable. - */ -let outdatedDbVersion: number | undefined; - -const walletInit: OpenedPromise = openPromise(); - -async function reinitWallet() { +async function reinitWallet(): Promise { if (currentWallet) { currentWallet.stop(); currentWallet = undefined; } currentDatabase = undefined; setBadgeText({ text: "" }); - const badge = new ChromeBadge(); try { currentDatabase = await openTalerDatabase(indexedDB, reinitWallet); } catch (e) { @@ -461,7 +460,7 @@ try { * * Sets up all event handlers and other machinery. */ -export async function wxMain() { +export async function wxMain(): Promise { // Explicitly unload the extension page as soon as an update is available, // so the update gets installed as soon as possible. chrome.runtime.onUpdateAvailable.addListener((details) => { @@ -505,8 +504,13 @@ export async function wxMain() { chrome.tabs.onRemoved.addListener((tabId, changeInfo) => { const tt = tabTimers[tabId] || []; + const bgPage = chrome.extension.getBackgroundPage(); + if (!bgPage) { + console.error("background page unavailable"); + return; + } for (const t of tt) { - chrome.extension.getBackgroundPage()!.clearTimeout(t); + bgPage.clearTimeout(t); } }); chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { @@ -515,12 +519,7 @@ export async function wxMain() { } const timers: number[] = []; - const addRun = (dt: number) => { - const id = chrome.extension.getBackgroundPage()!.setTimeout(run, dt); - timers.push(id); - }; - - const run = () => { + const run = (): void => { timers.shift(); chrome.tabs.get(tabId, (tab) => { if (chrome.runtime.lastError) { @@ -538,10 +537,20 @@ export async function wxMain() { document.dispatchEvent(new Event("taler-probe-result")); } `; - injectScript(tab.id!, { code, runAt: "document_start" }, uri.href); + injectScript(tab.id, { code, runAt: "document_start" }, uri.href); }); }; + const addRun = (dt: number): void => { + const bgPage = chrome.extension.getBackgroundPage(); + if (!bgPage) { + console.error("no background page"); + return; + } + const id = bgPage.setTimeout(run, dt); + timers.push(id); + }; + addRun(0); addRun(50); addRun(300); -- cgit v1.2.3