createMessageDispatcher
creates a message listener which will call a method on the
handler
argument, based on the contents of the message that's passed from elsewhere.
It does this by injecting the (potentially error causing) method into a promise chain,
by sticking it inside a custom thenable,
which gives us much nicer error handling.
const handler = {
methodA() {
console.log("called methodA", arguments);
return 42;
}
}
createMessageDispatcher("handlerid", handler);
elsewhere...
dispatchExtMessage("handlerid", "methodA", 1, 2, 3).then((resp) => console.log(resp))
// in THIS context
//# { state: "resolved", value: 42 }
// in the OTHER context
//# called method A [1, 2, 3]