Skip to content

Commit

Permalink
Remove exported consts from excmds.ts
Browse files Browse the repository at this point in the history
If we remove exported consts from excmds and only leave functions,
we can import it and use it as
{ [key: string]: (...args: any) => any } } object in background.ts
and the compiler won't beat as for it.

I also notived .excmds_content.generated was imported twice in
content.ts and removed second import, i hope it won't break anything.
  • Loading branch information
treapster authored and bovine3dom committed Mar 14, 2023
1 parent c793dae commit 7d6fcc8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
10 changes: 8 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,16 @@ browser.tabs.onCreated.addListener(aucon.tabCreatedListener)

// An object to collect all of our statistics in one place.
const statsLogger: perf.StatsLogger = new perf.StatsLogger()
const messages = {
const messages: { [key: string]: { [key: string]: (...args: any) => any } } = {
excmd_background: excmds_background,
controller_background: controller,
performance_background: statsLogger,
// We need to present logger object as an [string -> function] map to not be beaten up
// by typescript.
// There are probably better ways to do it, but for now i did this dirty thing just to
// make it build
performance_background: statsLogger as any as {
[key: string]: (...args: any) => any
},
download_background: {
downloadUrl: download_background.downloadUrl,
downloadUrlAs: download_background.downloadUrlAs,
Expand Down
5 changes: 2 additions & 3 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ try {
}

const controller = await import("@src/lib/controller")
const excmds_content = await import("@src/.excmds_content.generated")
const hinting_content = await import("@src/content/hinting")
// Hook the keyboard up to the controller
const ContentController = await import("@src/content/controller_content")
Expand Down Expand Up @@ -111,14 +110,14 @@ const { tabTgroup } = await import("@src/lib/tab_groups")
const completion_providers = await import("@src/completions/providers")

controller.setExCmds({
"": excmds_content,
"": excmds,
ex: CmdlineCmds,
text: EditorCmds,
hint: hinting_content.getHintCommands(),
})
messaging.addListener(
"excmd_content",
messaging.attributeCaller(excmds_content),
messaging.attributeCaller(excmds),
)
messaging.addListener(
"controller_content",
Expand Down
6 changes: 3 additions & 3 deletions src/excmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ import { generator as KEY_MUNCHER } from "@src/content/controller_content"
*
* @hidden
*/
export const cmd_params = new Map<string, Map<string, string>>()
const cmd_params = new Map<string, Map<string, string>>()

/** @hidden */
const logger = new Logging.Logger("excmd")
Expand Down Expand Up @@ -1587,7 +1587,7 @@ export async function reloadhard(n = 1) {
// I went through the whole list https://developer.mozilla.org/en-US/Firefox/The_about_protocol
// about:blank is even more special
/** @hidden */
export const ABOUT_WHITELIST = ["about:license", "about:logo", "about:rights", "about:blank"]
const ABOUT_WHITELIST = ["about:license", "about:logo", "about:rights", "about:blank"]

/**
* Open a new page in the current tab.
Expand Down Expand Up @@ -2401,7 +2401,7 @@ export async function loadaucmds(cmdType: "DocStart" | "DocLoad" | "DocEnd" | "T
* command (gi)
* @hidden
*/
export const INPUTTAGS_selectors = `
const INPUTTAGS_selectors = `
input:not([disabled]):not([readonly]):-moz-any(
:not([type]),
[type='text'],
Expand Down
24 changes: 11 additions & 13 deletions src/lib/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,25 @@ export function attributeCaller(obj) {
}

interface TypedMessage<
Root,
Type extends keyof Root,
Command extends keyof Root[Type]
Type extends keyof Messages.Background,
Command extends keyof Messages.Background[Type]
> {
type: Type
command: Command
args: Parameters<Root[Type][Command]>
args: Parameters<Messages.Background[Type][Command]>
}

function backgroundHandler<
Root,
Type extends keyof Root,
Command extends keyof Root[Type]
Type extends keyof Messages.Background,
Command extends keyof Messages.Background[Type]
>(
root: Root,
message: TypedMessage<Root, Type, Command>,
): ReturnType<Root[Type][Command]> {
return root[message.type][message.command](...message.args)
root: Messages.Background,
message: TypedMessage<Type, Command>,
): ReturnType<Messages.Background[Type][Command]> {
return root[message.type][message.command](message.args)
}

export function setupListener<Root>(root: Root) {
export function setupListener(root: Messages.Background) {
browser.runtime.onMessage.addListener(
(message: any) => {
if (message.type in root) {
Expand All @@ -118,7 +116,7 @@ export async function message<
Command extends keyof Messages.Background[Type],
F extends((...args: any[]) => any) & Messages.Background[Type][Command]
>(type: Type, command: Command, ...args: Parameters<F>) {
const message: TypedMessage<Messages.Background, Type, Command> = {
const message: TypedMessage<Type, Command> = {
type,
command,
args,
Expand Down

0 comments on commit 7d6fcc8

Please sign in to comment.