Skip to content

Commit

Permalink
global name changed; readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
serglider committed Mar 5, 2021
1 parent 4c2beb6 commit a6614db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
# cross-origin-courier

description - todo
A wrapper over `Window.postMessage()` and `MessageChannel` to facilitate messaging between cross origin contexts.

## Usage

todo
The library exposes a connection function that returns a `Promise`. On a successful connection, the `Promise` is resolved with the `courier` object to be used to send and listen to messages. **Note**: the child frame should call the connection function first.

## Setup

Expand All @@ -24,10 +22,11 @@ npm i cross-origin-courier
```

```js
import createConnection from 'cross-origin-courier';
import connect from 'cross-origin-courier';
// ...
createConnection().then(courier => {
connect().then(courier => {
courier.listen(handler);
courier.send({ answer: 42 });
});

function handler(data) {
Expand All @@ -41,14 +40,12 @@ function handler(data) {
<script src="https://unpkg.com/cross-origin-courier"></script>
```

Loaded this way, the connection function is exposed under the following long-ish name

```js
window.createConnection().then(courier => {
courier.listen(handler);
window.createCrossOrigConnection().then(courier => {
// ...
});

function handler(data) {
// do your stuff
}
```

## Options
Expand All @@ -70,6 +67,15 @@ createConnection(options).then(courier => {
| `targetOrigin` | string | '\*' | todo |
| `isParent` | boolean | false | todo |

## Courier API

| Method | Arguments | Description |
| -------: | :---------------------------------------: | :------------------------------------------- |
| `send` | `data`<[any][structured_clone_algorithm]> | Sends data to the counterparty |
| `listen` | `dataHandler`<(data: any) => void> | Sets a handler for the counterparty messages |

[structured_clone_algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

## Documentation

Please find the full docs [here](https://serglider.github.io/CrossOriginCourier/)
Expand Down
2 changes: 1 addition & 1 deletion rollup/dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default [
{
input: 'src/index.ts',
output: {
name: 'CrossOriginCourier',
name: 'createCrossOrigConnection',
file: pkg.browser,
format: 'umd',
sourcemap: true,
Expand Down
2 changes: 1 addition & 1 deletion rollup/prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default [
{
input: 'src/index.ts',
output: {
name: 'CrossOriginCourier',
name: 'createCrossOrigConnection',
file: pkg.browser,
format: 'umd',
sourcemap: true,
Expand Down

0 comments on commit a6614db

Please sign in to comment.