summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhackademix2019-10-22 09:45:12 +0200
committerhackademix2019-10-25 23:19:48 +0100
commit534ab54c28dda4b889735d7689f20f035b3bec84 (patch)
treefda84c10348b50631fec09145abfbc5fe7a2041b
parent384b4b742390ea7830a9291a57808a443e8ca31e (diff)
downloadnoscript-534ab54c28dda4b889735d7689f20f035b3bec84.tar.gz
noscript-534ab54c28dda4b889735d7689f20f035b3bec84.tar.xz
noscript-534ab54c28dda4b889735d7689f20f035b3bec84.zip
More compatible Messages abstraction.
-rw-r--r--src/lib/Messages.js31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/lib/Messages.js b/src/lib/Messages.js
index 5443acd..0a47c42 100644
--- a/src/lib/Messages.js
+++ b/src/lib/Messages.js
@@ -2,10 +2,10 @@
{
let handlers = new Set();
- let dispatch = async (msg, sender) => {
+ let dispatch = (msg, sender) => {
let {__meta, _messageName} = msg;
if (!__meta) {
- // legacy message from embedder?
+ // legacy message from embedder or library? ignore it
if (!_messageName) {
debug(`Message not in NoScript-specific format: %s`, JSON.stringify(msg));
return undefined;
@@ -13,19 +13,32 @@
__meta = {name: _messageName};
}
let {name} = __meta;
- let answers = [];
+ let responderFound = false;
+ let exception = null;
for (let h of handlers) {
let f = h[name];
+
if (typeof f === "function") {
- answers.push(f(msg, sender));
+ let result;
+ try {
+ result = f(msg, sender);
+ } catch (e) {
+ error(e);
+ exception = e;
+ continue;
+ }
+ if (typeof result === "undefined") {
+ responderFound = true;
+ continue;
+ }
+ return (result instanceof Promise) ? result
+ : new Promise(r => r(result));
}
}
- if (answers.length) {
- return await (
- answers.length === 1 ? answers.pop(): Promise.all(answers)
- );
+ if (exception) throw exception;
+ if (!responderFound) {
+ debug("Warning: no handler for message %s %s in context %s", name, JSON.stringify(msg), document.URL);
}
- debug("Warning: no handler for message %s %s in context %s", name, JSON.stringify(msg), document.URL);
};
var Messages = {