Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nilsmehlhorn/ngx-operators
RxJS operators for Angular
https://github.com/nilsmehlhorn/ngx-operators
Last synced: 3 months ago
JSON representation
RxJS operators for Angular
- Host: GitHub
- URL: https://github.com/nilsmehlhorn/ngx-operators
- Owner: nilsmehlhorn
- License: mit
- Created: 2020-03-07T11:42:18.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-31T11:49:20.000Z (10 months ago)
- Last Synced: 2024-07-12T15:48:28.741Z (4 months ago)
- Language: TypeScript
- Size: 1.75 MB
- Stars: 135
- Watchers: 4
- Forks: 15
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-angular - ngx-operators - RxJS operators for Angular. (Table of contents / Third Party Components)
- fucking-awesome-angular - ngx-operators - RxJS operators for Angular. (Table of contents / Third Party Components)
README
[![npm-badge](https://img.shields.io/npm/v/ngx-operators.svg?style=flat-square)](https://www.npmjs.com/package/ngx-operators)
[![travis-badge](https://img.shields.io/travis/nilsmehlhorn/ngx-operators/master.svg?style=flat-square)](https://travis-ci.org/nilsmehlhorn/ngx-operators)
[![codecov-badge](https://codecov.io/gh/nilsmehlhorn/ngx-operators/branch/master/graph/badge.svg)](https://codecov.io/gh/nilsmehlhorn/ngx-operators)ngx-operators is a collection of helpful RxJS operators for Angular apps.
## Installation
Install via
```bash
npm i ngx-operators
```## Operators
### prepare
Returns an Observable that mirrors the source Observable, but will call a specified function when it's being subscribed to.
`prepare(callback: () => void): (source: Observable) => Observable`
**callback**: Function to be called when source is being subscribed to.
**Example**
```typescript
const source = of("value").pipe(prepare(() => console.log("subscribed")));
source.subscribe(); // 'subscribed'
```[**read more**](https://nils-mehlhorn.de/posts/indicating-loading-the-right-way-in-angular)
### indicate
Indicates whether the observable is currently loading (meaning subscription is active and it hasn't completed or errored).
`indicate(indicator: Subject): (source: Observable) => Observable`
**indicator**: Subject as target for indication
**Example**
```typescript
@Component({...})
export class UserComponent {
loading$ = new Subject()constructor(private userService: UserService) {}
create(name = "John Doe"): void {
this.userService.create(new User(name))
.pipe(indicate(this.loading$))
.subscribe()
}
}
``````typescript
Create User
Creating, please wait
```[**read more**](https://nils-mehlhorn.de/posts/indicating-loading-the-right-way-in-angular)
### throwForCodes
Maps Angular HTTP status codes to more semantic errors.
`throwForCodes(codeErrors: {[status: number]: () => Error}): (source: Observable) => Observable`
**codeErrors**: Object mapping HTTP codes to error providers
**Example**
```typescript
this.http.post("/users", newUser).pipe(
throwForCodes({
409: () => new Error("User already exists"),
400: () => new Error("Invalid user")
})
);
```### download
Transform HTTP events into an observable download for indicating progress.
`download(saver?: (b: Blob) => void): (source: Observable>) => Observable`
**saver**: Function for saving download when it's done. This could be `saveAs` from [FileSaver.js](https://github.com/eligrey/FileSaver.js). When no `saver` is provided the download won't be saved by this operator.
**Example**
```typescript
@Component({...})
export class AppComponent {download$: Observable
constructor(private http: HttpClient) {}
download() {
this.download$ = this.http.get('/users/123/report', {
reportProgress: true,
observe: 'events',
responseType: 'blob'
}).pipe(download(() => saveAs('report.pdf')))
}
}
``````html
Download```
[**read more**](https://nils-mehlhorn.de/posts/angular-file-download-progress)
### upload
Transform HTTP events into an observable upload for indicating progress.
`upload(): (source: Observable>) => Observable>`
**Example**
```typescript
@Component()
export class AppComponent {
upload$: Observable;constructor(private http: HttpClient) {}
upload(files: FileList | null) {
const file = files?.item(0);
if (!file) {
return;
}
const data = new FormData();
data.append("file", file);
this.upload$ = this.http
.post("/users/123/avatar", data, {
reportProgress: true,
observe: "events"
})
.pipe(upload());
}
}
``````html
```
[**read more**](https://nils-mehlhorn.de/posts/angular-file-upload-progress)
### ignoreNotFound
Ignores 404 error responses by instead completing the underlying observable.
Note: You can use [defaultIfEmpty](https://rxjs-dev.firebaseapp.com/api/operators/defaultIfEmpty) to provide a fallback value.
`ignoreNotFound(): (source: Observable) => Observable`
**Example**
```typescript
@Component({...})
export class AppComponent {
user$: Observableconstructor(private http: HttpClient) {}
ngOnInit() {
this.user$ = this.http.get('/users/123').pipe(
ignoreNotFound()
);
}
}
```### noZoneRunner
Runs an observable sequence outside of the [Angular zone](https://angular.io/guide/zone) so that change detection won't be triggered for intermediate, possibly async, operations. This way change detection can only be run when you actually change your view model. Initialize the runner by passing [NgZone](https://angular.io/api/core/NgZone) which you inject into your service or component. Then wrap your observable with the runner.
`noZoneRunner(zone: NgZone): (source: Observable) => Observable`
**Example**
```typescript
@Component({...})
export class AppComponent {constructor(private zone: NgZone,
private el: ElementRef) {}ngOnInit() {
const runner = noZoneRunner(this.zone)
runner(fromEvent(this.el.nativeElement, 'mousemove').pipe(
/* fromEvent & operations are outside zone, won't trigger change-detection */
)).subscribe(() => {
/* operations back inside zone, will trigger change-detection */
})
}
}
```### runOutsideZone / runInZone
Moves observable execution in and out of [Angular zone](https://angular.io/guide/zone).
`runOutsideZone(zone: NgZone): (source: Observable) => Observable`
`runInZone(zone: NgZone): (source: Observable) => Observable`
**Example**
```typescript
obs$
.pipe(
runOutsideZone(this.zone),
tap(() => console.log(NgZone.isInAngularZone())), // false
runInZone(this.zone)
)
.subscribe(() => console.log(NgZone.isInAngularZone())); // true
```