list files
fix build url
split build and test
JSON-RPC implemented using postMessage API
npm install @statianzo/pmrpc
import JsonRpc from './JsonRpc';
const rpc = new JsonRpc({
methods: {
greet: name => `Hello ${name}`
},
source: window
});
import JsonRpc from './JsonRpc';
const rpc = new JsonRpc({
destination: iframe.contentWindow
});
rpc
.call('greet', 'World')
.then(response => {
console.log(response); // "Hello World"
});
origin
- (Default: *
) Origin to send messagesmethods
- (Default: {}
) An object of exposed methods. Returning a Promise will defer responding until the promise has completedsource
- The source EventTarget to mount JSON rpc on (Window
, MessagePort
, etc)destination
- Target MessageEventSource to send requests toInvoke a remote rpc method with an array of arguments (like Function.apply
).
Returns a Promise
with the result of the request. Errors from the endpoint
will result in a rejected promise.
Invoke a remote rpc method with a variadic list of arguments (like
Function.call
). Returns a Promise
with the result of the request.
Errors from the endpoint will result in a rejected promise.
Mount JsonRpc to a source EventTarget (Window, MessagePort, Worker). This is
called automatically when a source
is passed to the constructor.
Detach the event listener from the JsonRpc instance's source