webext-messenger
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -11,2 +11,7 @@ /// <reference types="firefox-webext-browser" /> | ||
declare type Method = (this: MessengerMeta, ...args: Arguments) => Promise<unknown>; | ||
export interface Target { | ||
tab: number; | ||
frame?: number; | ||
} | ||
declare type WithTarget<TMethod> = TMethod extends (...args: infer PreviousArguments) => infer TReturnValue ? (target: Target, ...args: PreviousArguments) => TReturnValue : never; | ||
/** | ||
@@ -16,4 +21,9 @@ * Replicates the original method, including its types. | ||
*/ | ||
export declare function getContentScriptMethod<TType extends keyof MessengerMethods, TMethod extends MessengerMethods[TType], PublicMethod extends WithTarget<ActuallyOmitThisParameter<TMethod>>>(type: TType): PublicMethod; | ||
/** | ||
* Replicates the original method, including its types. | ||
* To be called in the sender’s end. | ||
*/ | ||
export declare function getMethod<TType extends keyof MessengerMethods, TMethod extends MessengerMethods[TType], PublicMethod extends ActuallyOmitThisParameter<TMethod>>(type: TType): PublicMethod; | ||
export declare function registerMethods(methods: Partial<MessengerMethods>): void; | ||
export {}; |
@@ -1,2 +0,1 @@ | ||
import isPromise from "is-promise"; | ||
import { deserializeError, serializeError } from "serialize-error"; | ||
@@ -33,5 +32,32 @@ const errorKey = "__webext_messenger_error_response__"; | ||
*/ | ||
export function getContentScriptMethod(type) { | ||
const publicMethod = async (target, ...args) => { | ||
var _a; | ||
// TODO: This will throw if the receiving end doesn't exist, | ||
// i.e. if registerMethods hasn't been called | ||
const response = await browser.tabs.sendMessage(target.tab, { | ||
// Guarantees that a message is meant to be handled by this library | ||
__webext_messenger__: true, | ||
type, | ||
args, | ||
}, { | ||
// Must be specified. If missing, the message would be sent to every frame | ||
frameId: (_a = target.frame) !== null && _a !== void 0 ? _a : 0, | ||
}); | ||
if (isObject(response) && errorKey in response) { | ||
throw deserializeError(response[errorKey]); | ||
} | ||
return response; | ||
}; | ||
return publicMethod; | ||
} | ||
/** | ||
* Replicates the original method, including its types. | ||
* To be called in the sender’s end. | ||
*/ | ||
export function getMethod(type) { | ||
const publicMethod = async (...args) => { | ||
const returnValue = browser.runtime.sendMessage({ | ||
// TODO: This will throw if the receiving end doesn't exist, | ||
// i.e. if registerMethods hasn't been called | ||
const response = await browser.runtime.sendMessage({ | ||
// Guarantees that a message is meant to be handled by this library | ||
@@ -42,7 +68,2 @@ __webext_messenger__: true, | ||
}); | ||
// TODO: Add test for this. The target must exist but registerMethod must have never been called | ||
if (!isPromise(returnValue)) { | ||
throw new Error("No methods were registered in the receiving end"); | ||
} | ||
const response = await returnValue; | ||
if (isObject(response) && errorKey in response) { | ||
@@ -49,0 +70,0 @@ throw deserializeError(response[errorKey]); |
{ | ||
"name": "webext-messenger", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Browser Extension component messaging framework", | ||
@@ -71,3 +71,2 @@ "keywords": [], | ||
"dependencies": { | ||
"is-promise": "^4.0.0", | ||
"serialize-error": "^8.1.0", | ||
@@ -74,0 +73,0 @@ "webext-detect-page": "^3.0.2", |
8731
3
109
- Removedis-promise@^4.0.0
- Removedis-promise@4.0.0(transitive)