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

@lacolaco/ngx-store

v5.0.0

Published

Minimal state management for Angular

Downloads

12

Readme

@lacolaco/ngx-store

Minimal state management library for Angular based on RxJS.

https://yarn.pm/@lacolaco/ngx-store

@lacolaco/ngx-store Dev Token

CircleCI

npm version

Install

$ yarn add @lacolaco/ngx-store

How to Use

Create a store

A "store" is an Angular service which hold its state.

  1. Define a class which extends Store<T>.
  2. Pass the initial state to super({ initialState });
  3. Decorate the class with @Injectable() as well as other usual Angular services.
import { Injectable } from '@angular/core';
import { Store } from '@lacolaco/ngx-store';

export interface State {
  count: number;
}

export const initialState: State = {
  count: 0,
};

// Also you can use your NgModule's `providers` array to provide this service.
@Injectable({ providedIn: 'root' })
export class CounterStore extends Store<State> {
  constructor() {
    super({ initialState });
  }
}

Use the store

  1. Inject your store.
  2. Select specific value from the store with selectValue.
@Component({
  selector: 'app-root',
  template: `
    <p>Counter: {{ count$ | async }}</p>
  `,
})
export class AppComponent implements OnInit {
  constructor(private counterStore: CounterStore) {}
}

selectValue((state: T) => U): Observable<U> : Observe selected state change

selectValue method is for mapping and memoize. Internally it uses RxJS's map and distinctUntilChanged operators.

@Component({
  selector: 'app-root',
  template: `
    <p>Counter: {{ count$ | async }}</p>
  `,
})
export class AppComponent implements OnInit {
  count$: Observable<number>;

  constructor(private counterStore: CounterStore) {
    this.count$ = this.counterStore.selectValue(state => state.count);
  }
}

valueChanges: Observable<T>: Observe whole state changes

valueChanges is a raw observable of the store.

@Component({
  selector: 'app-root',
  template: `
    <p>Counter: {{ count$ | async }}</p>
  `,
})
export class AppComponent implements OnInit {
  count$: Observable<number>;

  constructor(private counterStore: CounterStore) {
    // same to `selectValue`
    this.count$ = this.counterStore.valueChanges.pipe(
      map(state => state.count),
      distinctUntilChanged(),
    );
  }
}

updateValue((state: T) => T) void: Update the store with new state

updateValue takes a function which takes the current state and returns a new state.

@Component({
  selector: 'app-root',
  template: `
    <p>Counter: {{ count$ | async }}</p>
  `,
})
export class AppComponent implements OnInit {
  count$: Observable<number>;

  constructor(private counterStore: CounterStore) {
    this.count$ = this.counterStore.selectValue(state => state.count);
  }

  ngOnInit() {
    setInterval(() => {
      this.counterStore.updateValue(state => ({ count: state.count }));
    }, 1000);
  }
}

Store Options

Listen onChange event

A store dispatchs an event when its state has been updated.

@Injectable({ providedIn: 'root' })
export class CounterStore extends Store<State> {
  constructor() {
    super({
      initialState,
      onUpdate: change => {
        console.log(`Previous Value`, change.previousValue);
        console.log(`Current Value`, change.currentValue);
        console.log(`Action Name`, change.actionName);
      },
    });
  }
}

actionName is the name of the function passed to updateValue. If you pass an anonymous function, change.actionName is empty. Named function is helpful for better logging or other purposes.

// Named function
const incrementOne = (state: CounterState) => ({
  count: state.count + 1,
});

@Component({
  selector: 'app-root',
  template: `
    <p>Counter: {{ count$ | async }}</p>
  `,
})
export class AppComponent implements OnInit {
  count$: Observable<number>;

  constructor(private counterStore: CounterStore) {
    this.count$ = this.counterStore.selectValue(state => state.count);
  }

  ngOnInit() {
    setInterval(() => {
      this.counterStore.updateValue(incrementOne);
    }, 1000);
  }
}

License

MIT

Author

Suguru Inatomi a.k.a. lacolaco

Patreon: https://www.patreon.com/lacolaco