Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sibnerian committed Apr 14, 2019
1 parent 0bdaff1 commit 1f3b34b
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Promise-y IPC calls in Electron.

## Installation

```sh
npm install --save electron-promise-ipc
```
Expand All @@ -25,21 +26,22 @@ promiseIpc.on('writeSettingsFile', (newSettings) => {
// in renderer
import promiseIpc from 'electron-promise-ipc';

promiseIpc.send('writeSettingsFile', '{ "name": "Jeff" }')
 .then(() => console.log('You wrote the settings!'))
.catch(e => console.error(e));
promiseIpc
.send('writeSettingsFile', '{ "name": "Jeff" }')
.then(() => console.log('You wrote the settings!'))
.catch((e) => console.error(e));
```

You can also send data from the main process to a renderer, if you pass in its [WebContents](http://electron.atom.io/docs/api/web-contents) object.


```js
// in main process
import promiseIpc from 'electron-promise-ipc';

promiseIpc.send('getRendererData', webContentsForRenderer)
.then(rendererData => console.log(rendererData))
.catch(e => console.error(e));
promiseIpc
.send('getRendererData', webContentsForRenderer)
.then((rendererData) => console.log(rendererData))
.catch((e) => console.error(e));

// in renderer
import promiseIpc from 'electron-promise-ipc';
Expand All @@ -55,6 +57,8 @@ Note that because this is IPC, only JSON-serializable values can be passed as ar

## Advanced usage

#### Timeouts

By default, the promise will wait forever for the other process to return it some data. If you want to set a timeout (after which the promise will be rejected automatically), you can create another instance of `PromiseIpc` like so:

```js
Expand All @@ -70,9 +74,24 @@ import { PromiseIpc } from 'electron-promise-ipc';

const promiseIpc = new PromiseIpc({ maxTimeoutMs: 2000 });

promiseIpc.send('someRoute', '{ "name": "Jeff" }')
 .then(() => console.log('You wrote the settings!'))
.catch(e => console.error(e)); // will error out after 2 seconds
promiseIpc
.send('someRoute', '{ "name": "Jeff" }')
.then(() => console.log('You wrote the settings!'))
.catch((e) => console.error(e)); // will error out after 2 seconds
```

#### Removing Listeners

You can remove a listener with the `off()` method. It's aliased to `removeListener()` as well.

```js
import promiseIpc from 'electron-promise-ipc';

promiseIpc.on('someRoute', () => {
return something();
});

promiseIpc.off('someRoute'); // never mind
```

## License
Expand Down

0 comments on commit 1f3b34b

Please sign in to comment.