Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add breaking changes and update imports #29

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,39 @@

Handle continuous stream of events with Promise and generator function.

[![npm version](https://badge.fury.io/js/event-as-promise.svg)](https://badge.fury.io/js/event-as-promise) [![Build Status](https://travis-ci.org/compulim/event-as-promise.svg?branch=master)](https://travis-ci.org/compulim/event-as-promise)
[![npm version](https://badge.fury.io/js/event-as-promise.svg)](https://npmjs.com/package/event-as-promise)

There are multiple alternatives, for example, [p-event](https://npmjs.com/package/p-event) is a popular choice.
Instead of listen to event *just once*, `event-as-promise` chose an approach to allow listening to the same event continuously. And we support *generator function* to enable `for await (const data of eventAsPromise)` loop to handle event indefinitely.

Instead of listen to event *just once*, `event-as-promise` chose an approach to allow listening to the same event continuously. And we use *generator function* to enable `for(of)` loop to handle event indefinitely.
# Breaking changes

## 2.0.0

We moved default imports to named imports:

```diff
- import EventAsPromise from 'event-as-promise';
+ import { EventAsPromise } from 'event-as-promise';
```

We removed `options: { array: boolean }`, to receive all arguments from Node.js event emitter:

```diff
- target.on(eventAsPromise.eventListener);
+ target.on((...args) => eventAsPromise.eventListener(args));
```

Only `eventListener` is bound to the instance of `EventAsPromise`. Other functions (`one` and `upcoming`) are not bound and will need to be call in the context of `EventAsPromise`. If you want to call it bound:

```diff
const eventAsPromise = new EventAsPromise();
- const one = eventAsPromise.one;
+ const one = eventAsPromise.one.bind(eventAsPromise)

button.addEventListener('click', eventAsPromise.eventListener);

await one();
```

# How to use

Expand All @@ -15,17 +43,17 @@ Instead of listen to event *just once*, `event-as-promise` chose an approach to
This sample code is converted from [Node about page](https://nodejs.org/en/about/).

```js
import EventAsPromise from 'event-as-promise';
import { EventAsPromise } from 'event-as-promise';
import http from 'http';

async function main(ready) {
const server = http.createServer();
const listeningPromises = new EventAsPromise();
const requestPromises = new EventAsPromise({ array: true });
const requestPromises = new EventAsPromise();

server
.once('listening', listeningPromises.eventListener)
.on('request', requestPromises.eventListener)
.on('request', (...args) => requestPromises.eventListener(args))
.listen(3000);

// Wait for "listening"
Expand All @@ -45,8 +73,6 @@ async function main(ready) {
main();
```

> Note: as multiple results is not supported by Promise, the `array` option will group multiple event arguments into an array. By default, it is `false`.

## Redux Saga

Handling event in a Promise may not reduce complexity. But it will be beneficial for [`redux-saga`](https://redux-saga.js.org/) when mixed with [`call`](https://redux-saga.js.org/docs/api/#callfn-args) effect.
Expand All @@ -57,12 +83,12 @@ In this example, when the user is connected via `CONNECTED` action, we will keep
saga.run(function* () {
yield takeLatest('CONNECTED', function* (action) {
const watcher = fs.watch(action.payload);
const changePromises = new EventAsPromise({ array: true });
const changePromises = new EventAsPromise();

watcher.on('change', changePromises.eventListener);

for (;;) {
const [changes] = yield race([
const changes = yield race([
call(changePromises.one),
take('DISCONNECTED'),
]);
Expand Down
Loading