https://github.com/avin/commutator
Making RPC calls with postmessages
https://github.com/avin/commutator
Last synced: about 1 year ago
JSON representation
Making RPC calls with postmessages
- Host: GitHub
- URL: https://github.com/avin/commutator
- Owner: avin
- Created: 2022-06-06T13:40:59.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T21:28:22.000Z (about 2 years ago)
- Last Synced: 2024-11-10T23:07:08.768Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 218 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Commutator
Making RPC calls (asynchronous method calls) between browser windows or iframes.
## Install
```
npm install commutator
```
## Usage
**parent.js**
```js
import { Commutator } from 'commutator';
const rpc = new Commutator({
// The window you want to talk to:
target: myIframe.contentWindow,
// This should be unique for each of your producer<->consumer pairs:
serviceId: 'my-awesome-service',
});
rpc.expose('add', (data) => data.a + data.b);
```
Destroy the instance
```js
rpc.destroy();
```
Remove expose handler
```js
const handleAdd = (data) => data.a + data.b
// Expose
rpc.expose('add', handleAdd);
// Unexpose
rpc.unexpose('add', handleAdd);
```
**iframe.js**
```js
import { Commutator } from 'commutator';
const rpc = new Commutator({
target: window.parent,
serviceId: 'my-awesome-service',
});
rpc.call('add', { a: 3, b: 5 }).then(result => console.log('3 + 5 is', result));
```