webext-messenger
Advanced tools
Comparing version 0.7.1 to 0.8.0
@@ -0,1 +1,2 @@ | ||
import pRetry from "p-retry"; | ||
import { deserializeError, serializeError } from "serialize-error"; | ||
@@ -31,3 +32,15 @@ const __webext_messenger__ = true; | ||
} | ||
async function handleResponse(response) { | ||
async function handleCall(type, sendMessage) { | ||
const response = await pRetry(sendMessage, { | ||
minTimeout: 100, | ||
factor: 1.3, | ||
maxRetryTime: 4000, | ||
onFailedAttempt(error) { | ||
if ((error === null || error === void 0 ? void 0 : error.message) !== | ||
"Could not establish connection. Receiving end does not exist.") { | ||
throw error; | ||
} | ||
console.debug("Messenger:", type, "will retry"); | ||
}, | ||
}); | ||
if (!isMessengerResponse(response)) { | ||
@@ -49,2 +62,9 @@ // If the response is `undefined`, `registerMethod` was never called | ||
} | ||
function makeMessage(type, args) { | ||
return { | ||
__webext_messenger__, | ||
type, | ||
args, | ||
}; | ||
} | ||
/** | ||
@@ -55,15 +75,8 @@ * Replicates the original method, including its types. | ||
export function getContentScriptMethod(type) { | ||
const publicMethod = async (target, ...args) => { | ||
const publicMethod = async (target, ...args) => handleCall(type, async () => { | ||
var _a; | ||
const response = await browser.tabs.sendMessage(target.tabId, { | ||
// Guarantees that a message is meant to be handled by this library | ||
__webext_messenger__, | ||
type, | ||
args, | ||
}, { | ||
// Must be specified. If missing, the message would be sent to every frame | ||
frameId: (_a = target.frameId) !== null && _a !== void 0 ? _a : 0, | ||
}); | ||
return handleResponse(response); | ||
}; | ||
return browser.tabs.sendMessage(target.tabId, makeMessage(type, args), | ||
// `frameId` must be specified. If missing, the message would be sent to every frame | ||
{ frameId: (_a = target.frameId) !== null && _a !== void 0 ? _a : 0 }); | ||
}); | ||
return publicMethod; | ||
@@ -76,11 +89,3 @@ } | ||
export function getMethod(type) { | ||
const publicMethod = async (...args) => { | ||
const response = await browser.runtime.sendMessage({ | ||
// Guarantees that a message is meant to be handled by this library | ||
__webext_messenger__, | ||
type, | ||
args, | ||
}); | ||
return handleResponse(response); | ||
}; | ||
const publicMethod = async (...args) => handleCall(type, async () => browser.runtime.sendMessage(makeMessage(type, args))); | ||
return publicMethod; | ||
@@ -87,0 +92,0 @@ } |
{ | ||
"name": "webext-messenger", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "Browser Extension component messaging framework", | ||
@@ -71,2 +71,3 @@ "keywords": [], | ||
"dependencies": { | ||
"p-retry": "^4.6.1", | ||
"serialize-error": "^8.1.0", | ||
@@ -73,0 +74,0 @@ "type-fest": "^2.3.4", |
9552
126
5
+ Addedp-retry@^4.6.1
+ Added@types/retry@0.12.0(transitive)
+ Addedp-retry@4.6.2(transitive)
+ Addedretry@0.13.1(transitive)