npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-operators

v8.0.0

Published

RxJS operators for Angular

Downloads

747

Readme

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