Package Exports
- better-postmessage
- better-postmessage/lib/cjs/index.js
- better-postmessage/lib/esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (better-postmessage) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Better postMessage
Tired of the window.postMessage()
method ? Need fresh new API that support Promises, answers and simple typed handling ?
BetterPostMessage will resolve all of that !
Installation
# NPM
npm i --save better-postmessage
# YARN
yarn i --save better-postmessage
# BUN
bun i --save better-postmessage
Usage
First context
import BetterPostMessage from "better-postmessage";
const messenger = new BetterPostMessage<string>(window);
messenger
.post({ message: "ping" })
.answer.then((answer) => {
if (answer === "pong") console.log("Second context is online !");
})
.catch(() => {
console.log("Second context has not responded");
});
Second context
import BetterPostMessage from "better-postmessage";
const messenger = new BetterPostMessage<string>(window);
messenger.onReceive(({ message }) => {
if (message === "ping") return "pong";
});
Parameters
Constructor arguments
new BetterPostMessage(context, options?)
Context
The Window-based context where to emit/receive messages
Options?
Not required
The object containing all behavior options of the messenger. Here are the different options :
KEY | DESCRIPTION | TYPE | DEFAULT VALUE |
---|---|---|---|
tunnel | Specify the custom tunnel of the proxy. Handler and message will be only receive/send to this tunnel | string |
"" |
answerTimeout | In milliseconds, the maximum amount of time a message can wait for its answer | number |
15_000 |
debug | If you want to view what's appening behind the process | boolean |
false |