Package Exports
- postmsg-rpc
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 (postmsg-rpc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
postmsg-rpc
Tiny RPC over window.postMessage library
Install
npm install postmsg-rpcUsage
In the window you want to call from (the "client"):
import { caller } from 'postmsg-rpc'
// Create a function that uses postMessage to call a function in a different window
const getFruits = caller('getFruits')
const fruits = await getFruits() // Wait for the fruits to ripenIn the other window (the "server"):
import { expose } from 'postmsg-rpc'
const fruitService = { getFruits: () => new Promise(/* ... */) }
// Map "calls" to this function name (over postMessage) to a function
expose('getFruits', fruitService.getFruits)API
caller(funcName, options)
Create a function that uses postMessage to call a function in a different window.
funcName- the name of the function to calloptions.targetOrigin- passed to postMessage (see postMessage docs for more info)- default
'*'
- default
The following options are for use with other similar messaging systems, for example when using message passing in browser extensions or for testing:
options.addListener- function that adds a listener- default
window.addEventListener
- default
options.removeListener- function that removes a listener- default
window.removeEventListener
- default
options.postMessage- function that posts a message- default
window.postMessage
- default
options.getMessageData- a function that extracts data from the event object passed to amessageevent handler- default
(e) => e.data
- default
The function returned from createClientFunc will return a Promise when called so can be awaited or used in the usual way (then/catch). The Promise returned has an additional property cancel which can be called to cancel an in flight request e.g.
const getFruits = caller('getFruits')
const fruitPromise = getFruits()
fruitPromise.cancel()
try {
await fruitPromise
} catch (err) {
if (err.isCanceled) {
console.log('function call canceled')
}
}expose(funcName, func, options)
Map "calls" to funcName (over postMessage) to a function. Assumes that the function being called on target returns a promise.
funcName- the name of the function called on the clientfunc- the function should be calledoptions.targetOrigin- passed to postMessage (see postMessage docs for more info)- default
'*'
- default
options.isCallback- set to true iffunctakes a node style callback instead of returning a promise- default
false
- default
The following options are for use with other similar messaging systems, for example when using message passing in browser extensions or for testing:
options.addListener- function that adds a listener- default
window.addEventListener
- default
options.removeListener- function that removes a listener- default
window.removeEventListener
- default
options.postMessage- function that posts a message- default
window.postMessage
- default
options.getMessageData- a function that extracts data from the event object passed to amessageevent handler- default
(e) => e.data
- default
Returns an object with a close method to stop the server from listening to messages.
A (╯°□°)╯︵TABLEFLIP side project.