mirror of
https://github.com/vale981/tridactyl
synced 2025-03-04 17:11:40 -05:00
Add strong typing to background messages
This commit is contained in:
parent
fbb3c9e50a
commit
a8b740b1bd
10 changed files with 70 additions and 39 deletions
|
@ -139,7 +139,7 @@ def background(lines, context):
|
||||||
return Messaging.message(
|
return Messaging.message(
|
||||||
"excmd_background",
|
"excmd_background",
|
||||||
"{sig.name}",
|
"{sig.name}",
|
||||||
[{message_params}],
|
{message_params}
|
||||||
)
|
)
|
||||||
}}\n""".format(**locals()))
|
}}\n""".format(**locals()))
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
/* tslint:disable:import-spacing */
|
/* tslint:disable:import-spacing */
|
||||||
|
|
||||||
import "@src/lib/browser_proxy_background"
|
import * as proxy_background from "@src/lib/browser_proxy_background"
|
||||||
|
|
||||||
import * as controller from "@src/lib/controller"
|
import * as controller from "@src/lib/controller"
|
||||||
import * as perf from "@src/perf"
|
import * as perf from "@src/perf"
|
||||||
|
@ -55,8 +55,6 @@ controller.setExCmds({
|
||||||
"text": EditorCmds,
|
"text": EditorCmds,
|
||||||
"hint": HintingCmds
|
"hint": HintingCmds
|
||||||
})
|
})
|
||||||
messaging.addListener("excmd_background", messaging.attributeCaller(excmds_background))
|
|
||||||
messaging.addListener("controller_background", messaging.attributeCaller(controller))
|
|
||||||
|
|
||||||
// {{{ tri.contentLocation
|
// {{{ tri.contentLocation
|
||||||
// When loading the background, use the active tab to know what the current content url is
|
// When loading the background, use the active tab to know what the current content url is
|
||||||
|
@ -165,10 +163,19 @@ browser.tabs.onCreated.addListener(
|
||||||
|
|
||||||
// An object to collect all of our statistics in one place.
|
// An object to collect all of our statistics in one place.
|
||||||
const statsLogger: perf.StatsLogger = new perf.StatsLogger()
|
const statsLogger: perf.StatsLogger = new perf.StatsLogger()
|
||||||
messaging.addListener(
|
export const messages = {
|
||||||
"performance_background",
|
excmd_background: excmds_background,
|
||||||
messaging.attributeCaller(statsLogger),
|
controller_background: controller,
|
||||||
)
|
performance_background: statsLogger,
|
||||||
|
download_background: {
|
||||||
|
downloadUrl: download_background.downloadUrl,
|
||||||
|
downloadUrlAs: download_background.downloadUrlAs,
|
||||||
|
},
|
||||||
|
browser_proxy_background: {shim: proxy_background.shim}
|
||||||
|
}
|
||||||
|
export type Messages = typeof messages
|
||||||
|
|
||||||
|
messaging.setupListener(messages)
|
||||||
// Listen for statistics from the background script and store
|
// Listen for statistics from the background script and store
|
||||||
// them. Set this one up to log directly to the statsLogger instead of
|
// them. Set this one up to log directly to the statsLogger instead of
|
||||||
// going through messaging.
|
// going through messaging.
|
||||||
|
|
|
@ -146,14 +146,3 @@ export async function downloadUrlAs(url: string, saveAs: string) {
|
||||||
browser.downloads.onChanged.addListener(onDownloadComplete)
|
browser.downloads.onChanged.addListener(onDownloadComplete)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
import * as Messaging from "@src/lib/messaging"
|
|
||||||
|
|
||||||
// Get messages from content
|
|
||||||
Messaging.addListener(
|
|
||||||
"download_background",
|
|
||||||
Messaging.attributeCaller({
|
|
||||||
downloadUrl,
|
|
||||||
downloadUrlAs,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
|
@ -839,9 +839,9 @@ export async function restart() {
|
||||||
//#content
|
//#content
|
||||||
export async function saveas(...filename: string[]) {
|
export async function saveas(...filename: string[]) {
|
||||||
if (filename.length > 0) {
|
if (filename.length > 0) {
|
||||||
return Messaging.message("download_background", "downloadUrlAs", [window.location.href, filename.join(" ")])
|
return Messaging.message("download_background", "downloadUrlAs", window.location.href, filename.join(" "))
|
||||||
} else {
|
} else {
|
||||||
return Messaging.message("download_background", "downloadUrl", [window.location.href, true])
|
return Messaging.message("download_background", "downloadUrl", window.location.href, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3962,7 +3962,7 @@ export async function hint(option?: string, selectors?: string, ...rest: string[
|
||||||
selectHints = hinting.pipe_elements(
|
selectHints = hinting.pipe_elements(
|
||||||
elems,
|
elems,
|
||||||
elem => {
|
elem => {
|
||||||
Messaging.message("download_background", "downloadUrl", [new URL(elem[attr], window.location.href).href, saveAs])
|
Messaging.message("download_background", "downloadUrl", new URL(elem[attr], window.location.href).href, saveAs)
|
||||||
return elem
|
return elem
|
||||||
},
|
},
|
||||||
rapid,
|
rapid,
|
||||||
|
@ -4074,7 +4074,7 @@ export function rot13(n: number) {
|
||||||
*/
|
*/
|
||||||
//#content
|
//#content
|
||||||
export function run_exstr(...commands: string[]) {
|
export function run_exstr(...commands: string[]) {
|
||||||
return Messaging.message("controller_background", "acceptExCmd", commands)
|
return Messaging.message("controller_background", "acceptExCmd", commands.join(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
|
@ -7,11 +7,11 @@ const browserProxy = new Proxy(Object.create(null), {
|
||||||
{
|
{
|
||||||
get(_, func) {
|
get(_, func) {
|
||||||
return (...args) =>
|
return (...args) =>
|
||||||
message("browser_proxy_background", "shim", [
|
message("browser_proxy_background", "shim",
|
||||||
api,
|
api,
|
||||||
func,
|
func,
|
||||||
args,
|
args,
|
||||||
])
|
)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
/** Shim to access BG browser APIs from content. */
|
/** Shim to access BG browser APIs from content. */
|
||||||
|
|
||||||
function shim(api, func, args) {
|
export function shim(api, func, args) {
|
||||||
return browser[api][func](...args)
|
return browser[api][func](...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
import { addListener, attributeCaller, MessageType } from "@src/lib/messaging"
|
|
||||||
addListener(
|
|
||||||
"browser_proxy_background" as MessageType,
|
|
||||||
attributeCaller({ shim }),
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { browserBg, activeTabId, ownTabId, getContext } from "@src/lib/webext"
|
import { browserBg, activeTabId, ownTabId, getContext } from "@src/lib/webext"
|
||||||
|
import * as Messages from "@src/message_protocols"
|
||||||
import Logger from "@src/lib/logging"
|
import Logger from "@src/lib/logging"
|
||||||
const logger = new Logger("messaging")
|
const logger = new Logger("messaging")
|
||||||
|
|
||||||
|
@ -65,9 +66,48 @@ export function attributeCaller(obj) {
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TypedMessage<Root, Type extends keyof Root, Command extends keyof Root[Type]> {
|
||||||
|
type: Type
|
||||||
|
command: Command
|
||||||
|
args: Parameters<Root[Type][Command]>
|
||||||
|
}
|
||||||
|
|
||||||
|
function backgroundHandler<
|
||||||
|
Root,
|
||||||
|
Type extends keyof Root,
|
||||||
|
Command extends keyof Root[Type]
|
||||||
|
>(root: Root,
|
||||||
|
message: TypedMessage<Root, Type, Command>,
|
||||||
|
sender: browser.runtime.MessageSender,
|
||||||
|
): ReturnType<Root[Type][Command]> {
|
||||||
|
return root[message.type][message.command](...message.args)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setupListener<Root>(root: Root) {
|
||||||
|
browser.runtime.onMessage.addListener((message: any, sender: browser.runtime.MessageSender) => {
|
||||||
|
if (message.type in root) {
|
||||||
|
if (!(message.command in root[message.type]))
|
||||||
|
throw new Error(`missing handler in protocol ${message.type} ${message.command}`)
|
||||||
|
if (!Array.isArray(message.args))
|
||||||
|
throw new Error(`wrong arguments in protocol ${message.type} ${message.command}`)
|
||||||
|
return backgroundHandler(root, message, sender)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** Send a message to non-content scripts */
|
/** Send a message to non-content scripts */
|
||||||
export async function message(type: NonTabMessageType, command, args?) {
|
export async function message<
|
||||||
return browser.runtime.sendMessage({ type, command, args } as Message)
|
Type extends keyof Messages.Background,
|
||||||
|
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> = {
|
||||||
|
type,
|
||||||
|
command,
|
||||||
|
args
|
||||||
|
}
|
||||||
|
|
||||||
|
return browser.runtime.sendMessage<typeof message, ReturnType<F>>(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Message the active tab of the currentWindow */
|
/** Message the active tab of the currentWindow */
|
||||||
|
|
3
src/message_protocols.ts
Normal file
3
src/message_protocols.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// This file re-exports types for message protocols for background, content and commandline contexts
|
||||||
|
|
||||||
|
export { Messages as Background } from "@src/background"
|
|
@ -47,6 +47,6 @@ window.addEventListener("load", _ => {
|
||||||
// Periodically nag people about updates.
|
// Periodically nag people about updates.
|
||||||
window.addEventListener("load", _ => {
|
window.addEventListener("load", _ => {
|
||||||
if (config.get("update", "nag") === true) {
|
if (config.get("update", "nag") === true) {
|
||||||
Messaging.message("controller_background", "acceptExCmd", ["updatecheck auto_polite"])
|
Messaging.message("controller_background", "acceptExCmd", "updatecheck auto_polite")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -416,7 +416,5 @@ class MetricName {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendStats(list: PerformanceEntryList) {
|
function sendStats(list: PerformanceEntryList) {
|
||||||
messaging.message("performance_background", "receiveStatsJson", [
|
messaging.message("performance_background", "receiveStatsJson", JSON.stringify(list))
|
||||||
JSON.stringify(list),
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue