ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - nilsmehlhorn/ngx-operators: RxJS operators for Angular
RxJS operators for Angular. Contribute to nilsmehlhorn/ngx-operators development by creating an account on GitHub.
Visit Site

GitHub - nilsmehlhorn/ngx-operators: RxJS operators for Angular

GitHub - nilsmehlhorn/ngx-operators: RxJS operators for Angular

npm-badge   travis-badge   codecov-badge

ngx-operators is a collection of helpful RxJS operators for Angular apps.

Installation

Install via

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<T>(callback: () => void): (source: Observable<T>) => Observable<T>

callback: Function to be called when source is being subscribed to.

Example

const source = of("value").pipe(prepare(() => console.log("subscribed")));
source.subscribe(); // 'subscribed'

read more

indicate

Indicates whether the observable is currently loading (meaning subscription is active and it hasn't completed or errored).

indicate<T>(indicator: Subject<boolean>): (source: Observable<T>) => Observable<T>

indicator: Subject as target for indication

Example

@Component({...})
export class UserComponent  {
  loading$ = new Subject<boolean>()

  constructor(private userService: UserService) {}

  create(name = "John Doe"): void {
    this.userService.create(new User(name))
    .pipe(indicate(this.loading$))
    .subscribe()
  }
}
<button (click)="create()">Create User</button>
<div *ngIf="loading$ | async">
  Creating, please wait <loading-indicator></loading-indicator>
</div>

read more

throwForCodes

Maps Angular HTTP status codes to more semantic errors.

throwForCodes<T>(codeErrors: {[status: number]: () => Error}): (source: Observable<T>) => Observable<T>

codeErrors: Object mapping HTTP codes to error providers

Example

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<HttpEvent<Blob>>) => Observable<Download>

saver: Function for saving download when it's done. This could be saveAs from FileSaver.js. When no saver is provided the download won't be saved by this operator.

Example

@Component({...})
export class AppComponent  {

  download$: Observable<Download>

  constructor(private http: HttpClient) {}

  download() {
    this.download$ = this.http.get('/users/123/report', {
                           reportProgress: true,
                           observe: 'events',
                           responseType: 'blob'
                         }).pipe(download(() => saveAs('report.pdf')))
  }
}
<button (click)="download()">Download</button>
<mat-progress-bar
  *ngIf="download$ | async as download"
  [mode]="download.state == 'PENDING' ? 'buffer' : 'determinate'"
  [value]="download.progress"
>
</mat-progress-bar>

read more

upload

Transform HTTP events into an observable upload for indicating progress.

upload<T = unknown>(): (source: Observable<HttpEvent<T>>) => Observable<Upload<T>>

Example

@Component()
export class AppComponent {
  upload$: Observable<Upload>;

  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());
  }
}
<input type="file" #fileInput (change)="upload(fileInput.files)" />
<mat-progress-bar
  *ngIf="upload$ | async as upload"
  [mode]="upload.state == 'PENDING' ? 'buffer' : 'determinate'"
  [value]="upload.progress"
>
</mat-progress-bar>

read more

ignoreNotFound

Ignores 404 error responses by instead completing the underlying observable.

Note: You can use defaultIfEmpty to provide a fallback value.

ignoreNotFound(): (source: Observable<T>) => Observable<T>

Example

@Component({...})
export class AppComponent  {
  user$: Observable<User>

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.user$ = this.http.get<User>('/users/123').pipe(
      ignoreNotFound()
    );
  }
}

noZoneRunner

Runs an observable sequence outside of the Angular 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 which you inject into your service or component. Then wrap your observable with the runner.

noZoneRunner(zone: NgZone): (source: Observable<T>) => Observable<T>

Example

@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.

runOutsideZone(zone: NgZone): (source: Observable<T>) => Observable<T>

runInZone(zone: NgZone): (source: Observable<T>) => Observable<T>

Example

obs$
  .pipe(
    runOutsideZone(this.zone),
    tap(() => console.log(NgZone.isInAngularZone())), // false
    runInZone(this.zone)
  )
  .subscribe(() => console.log(NgZone.isInAngularZone())); // true

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory