Skip to content

Commit

Permalink
Update AwaiEvent docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyyakym committed Dec 17, 2023
1 parent 691a116 commit ab69619
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions docs/docs/awai-event.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ slug: /awai-event

Awai event is a promise-like object which has no terminal state and may resolve multiple times. It plays role of event-emitter.

### Example of usage
### Methods

- **abortable** - method that receives AbortController instance and returns Promise which can be aborted using that controller. It is mainly introduced for internal usage, in order to free memory up as soon as possible
- **emit** - method to emit an event to all the awaiters
- **filter** - method that returns a Promise of an event for which predicate returns truthy value.

### Examples

#### Simple usage

```ts title="Example of AwaiEvent usage"
const event = new AwaiEvent();
Expand All @@ -25,10 +33,10 @@ Notice how we can await the `event` object multiple times and receive different


:::note Hint
AwaiEvent is promise-like, hence may be combined with other promise-like objects using Promise methods like `race`, `all` or used in any async function.
AwaiEvent is promise-like, hence may be combined with other promise-like objects using Promise methods like `race`, `all` or used in any async function.
:::

### Example of replacing event-emitter with re-resolvable
#### Replacing event-emitter with re-resolvable

In the very traditional approach you would attach event listener like this:

Expand All @@ -37,7 +45,7 @@ const eventHandler = (event) => /* handle event */;
document.addEventListener('click', eventHandler);
```

The same functionality may be achieved by the following code:
With AwaiEvent, you can write same functionality using async flow. Of course, at some places you would need to attach native events and proxy them to AwaiEvent, but then you can build whole app architecture using Awai approach.

```ts title="AwaiEvent way of listening to events"
const clickEvent = new AwaiEvent();
Expand All @@ -47,3 +55,26 @@ while (true) {
/* handle event */
}
```

The above snippet is only written for demonstration purposes. You should use [Scenario](/scenario) instead.

```ts title="AwaiEvent way of listening to events using Scenario"
const clickEvent = new AwaiEvent();

scenario(clickEventReResolvable, (event) => {
/* handle event */
});
```

#### Filter out events

```ts title="Handle Enter key only"
const keyDownEvent = new AwaiEvent();

scenario(
() => keyDownEvent.filter(event => event.key === 'Enter'),
(event) => {
/* handle event */
},
);
```

0 comments on commit ab69619

Please sign in to comment.