Skip to content

Commit

Permalink
extension: fix some previously ignored typescript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed May 28, 2024
1 parent 9400280 commit ad4c3b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
20 changes: 9 additions & 11 deletions extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import browser from "webextension-polyfill"
import type {BrowserAction, Menus, PageAction, Runtime, Tabs, WebNavigation} from "webextension-polyfill"

import type {Url, SearchPageParams} from './common'
import {Visit, Visits, Blacklisted, Methods, uuid} from './common'
import {Visit, Visits, Blacklisted, Methods, assert, uuid} from './common'
import type {Options} from './options'
import {Toggles, getOptions, setOption, THIS_BROWSER_TAG} from './options'

Expand Down Expand Up @@ -666,7 +666,10 @@ export async function handleToggleSidebar() {
function registerActions() {
// NOTE: on mobile, this sets action for both icon (if it's displayed) and in the menu
for (const action of actions()) {
action.onClicked.addListener(defensify(toggleSidebarOnTab, 'action.onClicked'))
action.onClicked.addListener(defensify(
(tab: Tabs.Tab) => toggleSidebarOnTab({url: tab.url!, id: tab.id!}),
'action.onClicked',
))
}
}

Expand All @@ -689,12 +692,6 @@ const onCommandCallback = defensify(async cmd => {
}, 'onCommand')


type MenuInfo = {
menuItemId: string,
linkUrl?: string,
}


async function active(): Promise<TabUrl> {
return (await getActiveTab())!
}
Expand Down Expand Up @@ -844,7 +841,7 @@ ${surl}
},
)
},
onMenuClick: async function(info: MenuInfo, _tab: Tabs.Tab) {
onMenuClick: async function(info: Menus.OnClickData, _tab: Tabs.Tab) {
const url = info.linkUrl!
await AddToMarkVisitedExcludelist.add([url])
},
Expand All @@ -858,7 +855,7 @@ const DEFAULT_CONTEXTS: Array<Menus.ContextType> = ['page', 'browser_action']
type MenuEntry = {
id: string,
title: string,
callback: ((info: MenuInfo, tab: Tabs.Tab) => Promise<void>) | null,
callback: ((info: Menus.OnClickData, tab: Tabs.Tab) => Promise<void>) | null,
contexts?: Array<Menus.ContextType>, // NOTE: not present interpreted as DEFAULT_CONTEXTS
parentId?: string,
}
Expand Down Expand Up @@ -977,7 +974,8 @@ function initContextMenus(): void {
})

// need to keep these callbacks here, since onInstalled above isn't called when background page resumes
const onMenuClickedCallback = defensify(async (info: MenuInfo, tab: Tabs.Tab) => {
const onMenuClickedCallback = defensify(async (info: Menus.OnClickData, tab: Tabs.Tab | undefined) => {
assert(tab != null)
const mid = info.menuItemId
for (const m of [...MENUS, ...TOGGLES]) {
if (mid == m.id) {
Expand Down
8 changes: 8 additions & 0 deletions extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,11 @@ export function getOrDefault<T>(obj: any, key: string, def: T): T {
const res = obj[key];
return res === undefined ? def : res;
}


// js doesn't have builtin assert :(
export function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new Error(`assertion failed ${msg}`)
}
}
21 changes: 12 additions & 9 deletions extension/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export function alertError(obj: any) {
}


// TODO see if I can define a typescript tyoe..
// @ts-expect-error
export function defensify(pf: (...any) => Promise<any>, name: string): (...any) => Promise<void> {
const fname = pf.name // hopefully it's always present?
type DefensifyArgs = readonly unknown[]
export function defensify<Args extends DefensifyArgs>(
pf: (...args: Args) => Promise<any>,
name: string,
): (...args: Args) => Promise<void> {
const fname = pf.name // hopefully it's always present?
const error_handler = (err: Error) => {
console.error('function "%s" %s failed: %o', fname, name, err)
getOptions().then((opts: Options) => {
Expand All @@ -56,17 +58,18 @@ export function defensify(pf: (...any) => Promise<any>, name: string): (...any)
}
})
}
return (...args) => pf(...args).then(() => {
return (...args: Args) => pf(...args).then(() => {
// suppress return values, since the defensive error handler 'erases; the type anyway'
return
}).catch(error_handler)
}


// TODO return type should be void here too...
// @ts-expect-error
export function defensifyAlert(pf: (...any) => Promise<any>): (any) => Promise<any> {
return (...args) => pf(...args).catch(alertError);
type DefensifyAlertArgs = readonly unknown[]
export function defensifyAlert<Args extends DefensifyAlertArgs>(
pf: (...args: Args) => Promise<any>,
): (...args: Args) => Promise<void> {
return (...args) => pf(...args).catch(alertError)
}


Expand Down
3 changes: 3 additions & 0 deletions extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"moduleResolution": "bundler",
// esnext is necessary, otherwise bundler module resolution can't be used?
"module": "esnext",

// without it, emacs (LSP?) complains when editing files.. not sure if impacts actual code generation?
"lib": ["es6", "dom"],
},
"include": [
"./src/**/*.ts"
Expand Down

0 comments on commit ad4c3b5

Please sign in to comment.