webext-messenger
Advanced tools
Comparing version 0.0.1 to 0.1.0
/// <reference types="firefox-webext-browser" /> | ||
declare type Arguments = any[]; | ||
declare type Method = (this: browser.runtime.MessageSender, ...args: Arguments) => Promise<unknown>; | ||
export declare type Contract<TMmethod extends Method = Method> = { | ||
type: string; | ||
method?: TMmethod; | ||
}; | ||
/** | ||
* Registers a handler for a specific method. | ||
* Returns a function that registers a handler for a specific method. | ||
* To be called in the receiving end. | ||
*/ | ||
export declare function registerMethod<TContract extends Required<Contract>>(type: TContract["type"], handler: TContract["method"]): void; | ||
export declare function getRegistration(type: string, method: Method): () => void; | ||
/** | ||
@@ -17,3 +13,3 @@ * Replicates the original method, including its types. | ||
*/ | ||
export declare function getMethod<TContract extends Contract, LocalMethod extends OmitThisParameter<NonNullable<TContract["method"]>>>({ type }: TContract): LocalMethod; | ||
export declare function getMethod<TMethod extends Method>(type: string): OmitThisParameter<TMethod>; | ||
export {}; |
@@ -24,11 +24,13 @@ function isObject(value) { | ||
/** | ||
* Registers a handler for a specific method. | ||
* Returns a function that registers a handler for a specific method. | ||
* To be called in the receiving end. | ||
*/ | ||
export function registerMethod(type, handler) { | ||
if (handlers.has(type)) { | ||
throw new Error(`Handler already set for ${type}`); | ||
} | ||
handlers.set(type, handler); | ||
browser.runtime.onMessage.addListener(onMessageListener); | ||
export function getRegistration(type, method) { | ||
return () => { | ||
if (handlers.has(type)) { | ||
throw new Error(`Handler already set for ${type}`); | ||
} | ||
handlers.set(type, method); | ||
browser.runtime.onMessage.addListener(onMessageListener); | ||
}; | ||
} | ||
@@ -39,3 +41,3 @@ /** | ||
*/ | ||
export function getMethod({ type }) { | ||
export function getMethod(type) { | ||
return (async (...args) => browser.runtime.sendMessage({ | ||
@@ -42,0 +44,0 @@ type, |
{ | ||
"name": "webext-messenger", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Browser Extension component messaging framework", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
6275
59