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-reactivetoolkit

v10.0.0

Published

Angular is a framework that embraces reactive programming, in particular RXJS. This project is to help you embrace reactive programming even more.

Downloads

2,141

Readme

Reactive toolkit

Angular is a framework that embraces reactive programming, in particular RXJS. This project is to help you embrace reactive programming even more.

The toolkit is pretty small for now and only comes with a handful of helpers and utils. It will get updated in the future to help us even more. If you want to contribute or have great ideas... Please do not hesitate to request changes.

Installation

npm install --save ngx-reactivetoolkit

Documentation

Versions

Angular 13: 10.x.x

Angular 12: 9.x.x

Angular 11: 9.x.x

Angular 10: 9.x.x

Angular 9: 8.x.x

Angular 8: 7.x.x, 6.x.x

Angular 7: 5.x.x

Angular 6: 4.x.x

Angular 5: 3.x.x, 2.x.x and 1.x.x

Angular 4: 3.x.x, 2.x.x and 1.x.x

The Destroy decorator

When using streams in angular you have to make sure that you are unsubscribing to your streams. When using async pipes those particular streams are already getting unsubscribed for you automatically. But in a bunch of cases it is still needed to subscribe to streams at component level. In that case there are two things you can do:

  • Keep track of all subscriptions and destroy them at ngOnDestroy
  • Keep track of a destroy stream and use the takeUntil operator

The Destroy decorator covers that logic for you.

An cleaner alternative to the destroy decorator is the takeUntilDestroy operator also available in this library.

import {Destroy} from 'ngx-reactivetoolkit';

@Component({
    selector: 'my-component',
    template: `...`,
})
export class HelloComponent implements OnDestroy {
    // by using the @Destroy annotation a stream will be created for you
    // and will get a true value when the component gets destroyed
    @Destroy() destroy$;

    constructor() {
        interval(500).pipe(
            // be safe and use the created destroy$ to stop the stream automatically
            takeUntil(this.destroy$)
        ).subscribe(e => console.log(e));
    }

    // because of aot we need to implement the ngOnDestroy method for @Destroy to work
    ngOnDestroy(): void {}
}

The Changes decorator

What if a component gets a lot of inputs and we need to combine all these values. Wouldn't it be great if we could just create streams of these values and combine them where we want to. That way we could start writing reactive code in dumb components as well.

The changes decorator covers that logic for you.

import {Changes} from 'ngx-reactivetoolkit';

@Component({
    selector: 'my-component',
    template: `...`,
})
export class HelloComponent implements OnChanges {
    @Input() a;
    @Input() b;
    // by using the @Changes annotation a stream will be created for you
    // and will get a new value every time an input of a component changes
    @Changes() changes$;

    constructor(){
        this.changes$.subscribe(e => console.log(e));
    }
    a$ = this.changes$.filter(changes => changes.a).map(changes => changes.a.currentValue);
    b$ = this.changes$.filter(changes => changes.b).map(changes => changes.b.currentValue);

    // because of aot we need to implement the ngOnChanges method for @Changes to work
    ngOnChanges(): void {}
}

You could also pass the name of an input to create a stream directly from that input as well as define a starting value.

import {Changes} from 'ngx-reactivetoolkit';

@Component({
    selector: 'my-component',
    template: `...`,
})
export class HelloComponent implements OnChanges {
    @Input() a;
    @Input() b;
    @Changes('a') a$; // will get nexted every time a changes
    @Changes('b', 100) b$; // will get nexted every time b changes, and will start with the value 100 

    // because of aot we need to implement the ngOnChanges method for @Changes to work
    ngOnChanges(): void {}
}

The takeUntilDestroy operator

The takeUntilDestroy operator is a cleaner alternative over the destroy decorator. The implementation is mostly identical to the one of Netanel Basal. Credits to him and his team.

When using streams in angular you have to make sure that you are unsubscribing to your streams. When using async pipes those particular streams are already getting unsubscribed for you automatically. But in a bunch of cases it is still needed to subscribe to streams at component level. In that case there are two things you can do:

  • Keep track of all subscriptions and destroy them at ngOnDestroy
  • Keep track of a destroy stream and use the takeUntil operator

The takeUntilDestroy operator covers that logic for you.

import {UntilDestroy, takeUntilDestroy} from 'ngx-reactivetoolkit';

@UntilDestroy()
@Component({
    selector: 'my-component',
    template: `...`,
})
export class HelloComponent {
    constructor() {
        interval(500).pipe(
            takeUntilDestroy(this)
        ).subscribe(e => console.log(e));
    }
}