Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into bot/publish-release/2.0.1-0
Browse files Browse the repository at this point in the history
compulim authored Jun 2, 2024
2 parents 2f29d75 + 9d4f5fd commit 5b10414
Showing 5 changed files with 55 additions and 21 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [2.0.0] - 2024-06-02

### Changed

@@ -33,7 +33,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [`redux@4.0.5`](https://npmjs.com/package/redux)
- [`redux-saga@1.1.3`](https://npmjs.com/package/redux-saga)
- [`regenerator-runtime@0.13.5`](https://npmjs.com/package/regenerator-runtime)

- Bump dependencies, in PR [#5](https://github.com/compulim/event-as-promise/pull/5)
- [@babel/cli@^7.5.5](https://www.npmjs.com/package/@babel/cli)
- [@babel/core@^7.5.5](https://www.npmjs.com/package/@babel/core)
@@ -87,3 +86,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- First public release

[2.0.0]: https://github.com/compulim/event-as-promise/compare/v1.1.0...v2.0.0
[1.1.0]: https://github.com/compulim/event-as-promise/compare/v1.0.5...v1.1.0
[1.0.5]: https://github.com/compulim/event-as-promise/compare/v1.0.4...v1.0.5
[1.0.4]: https://github.com/compulim/event-as-promise/compare/v1.0.3...v1.0.4
[1.0.3]: https://github.com/compulim/event-as-promise/compare/v1.0.2...v1.0.3
[1.0.2]: https://github.com/compulim/event-as-promise/compare/v1.0.1...v1.0.2
[1.0.1]: https://github.com/compulim/event-as-promise/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/compulim/event-as-promise/releases/tag/v1.0.0
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

@@ -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"
@@ -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.
@@ -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'),
]);
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/event-as-promise/package.json
Original file line number Diff line number Diff line change
@@ -57,10 +57,10 @@
"@tsconfig/recommended": "^1.0.6",
"@tsconfig/strictest": "^2.0.5",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.13",
"@types/node": "^20.14.0",
"esbuild": "^0.21.4",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"prettier": "^3.3.0",
"tsup": "^8.0.2",
"typescript": "^5.4.5"
}
2 changes: 1 addition & 1 deletion packages/integration-test/package.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
"@babel/preset-env": "^7.24.6",
"@babel/preset-react": "^7.24.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.13",
"@types/node": "^20.14.0",
"jest": "^29.7.0"
},
"dependencies": {

0 comments on commit 5b10414

Please sign in to comment.