An open API service indexing awesome lists of open source software.

https://github.com/julienblin/rx-async-event


https://github.com/julienblin/rx-async-event

angular angular2 angular4 npm rxjs rxjs-observables

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

# rx-async-event

Manages processing / processed / error state (mainly) for Angular applications.

[![Travis](https://travis-ci.org/julienblin/rx-async-event.svg?branch=master)](https://travis-ci.org/julienblin/rx-async-event)
[![npm](https://img.shields.io/npm/v/rx-async-event.svg)](https://www.npmjs.com/package/rx-async-event)

## Objectives

This is a small helper library to help manage processing and processed states for Angular applications.
It allows encapsulation of either a promise or an observable, and emits standardized life cycle events:
- init
- processing
- processed
- error

As a side benefit, it also manages errors so that they bubble up in a managed way (instead of crashing the application).

## How to use

### Installation

```shell
npm i --save rx-async-event
```

### Service

```typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AsyncEventObservable, AsyncEventSubject } from 'rx-async-event';
import { Post } from './post';

@Injectable()
export class AppService {

private readonly _postEvent$ = new AsyncEventSubject();

constructor(private http: HttpClient) { }

get postEvent$(): AsyncEventObservable {
return this._postEvent$.asObservable();
}

/**
* Example of managing the life cycle of an HttpClient Observable.
*/
loadPost(id: number) {
return this._postEvent$.observe(
id,
this.http.get(`https://jsonplaceholder.typicode.com/posts/${id}`));
}

/**
* This example manages the life cycle of a Promise instead.
*/
loadPostAsPromise(id: number) {
return this._postEvent$.execute(
id,
(arg) => this.http.get(`https://jsonplaceholder.typicode.com/posts/${id}`).toPromise());
}


}
```

### Component

```typescript
import { Component } from '@angular/core';
import { AppService } from './app.service';
import { Post } from './post';
import { AsyncEventObservable } from 'rx-async-event';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [ AppService ]
})
export class AppComponent {

postEvent$: AsyncEventObservable;

constructor(private appService: AppService) {
this.postEvent$ = this.appService.postEvent$;
}

loadPost(id: number) {
this.appService.loadPost(id);
}
}
```

### View

```html



Nothing to display



Processing post id {{postEvent.argument}}...



Post title: {{postEvent.result.title}}



Ohuh - something went wrong: {{postEvent.error.message}}

```