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

angular-reactive-state

v2.0.0

Published

Reactive state management library for Angular

Downloads

7

Readme

Advantages

  • Provide a store for each Angular module
  • Flexible and customizable store services
  • Supports Redux Dev Tool Extensions

Getting started

Installation

NPM

npm i angular-reactive-state

Yarn

yarn add angular-reactive-state

Create a store

As we are following a decentralized store concept we create a separate store for each of our modules:

Run the following command to create a store service:

ng generate service <store-name>

Now we follow these steps to transform the generated into a reactive store:

  • the service class extends the Store class
  • the Store class wants the type information of how our state looks like
  • the super function adds a name for the store and the initial state

Example

// todo-store.service.ts
import { Injectable } from '@angular/core';
import { Store } from 'angular-reactive-state';

type TodoStoreState = {
  todos: string[];
};

@Injectable({
  providedIn: 'root',
})
export class TodoStoreService extends Store<TodoStoreState> {
  constructor() {
    super('TodoStore', {
      todos: [],
    });
  }
}

Subscribe the state

The UI components can inject the store service like this:

constructor(private todoStore: TodoStoreService) {}

To subscribe to the state of the todoStore there are different possbilities:

  • subscribe to the whole state at once
  • subscribe to a specific part of the state

Example

// subscribe to the whole state at once
this.todoStore.state$.subscribe(state => {
  console.log(state); // output: { todos: [] }
});

// subscribe to a specific part of the state
this.todoStore
  .select(state => state.todos)
  .subscribe(todos => {
    console.log(todos); // output: []
  });

The select method will only trigger an event if the value of the specific property of the state has been changed. In addition the returned values are deep copies of the values in the store, so it is can't cause any reference issues.

Usage with signals

Since Angular 17, Signals can be used instead of Observables. To achieve this, the function selectAsSignal can be used:

// get a signal of a specific part of the state
const myTodos = this.todoStore.selectAsSignal(state => state.todos);

Get state snapshot

Some operations only require the current state of the store and do not need to get notified about changes. These are cases where a snapshot of the latest state can be very helpful.

Example

// get a part of the state without subscribing to it
console.log(this.todoStore.snapshot.todos); // output: ['my todo']

Change the state

There are two possibilities to update the state in the store:

  • update a root-level property of the state
  • update the whole state at once

Example

// replace a root-level property of the state with a new value
this.todoStore.updateProperty('todos', ['my first todo'], 'add todo');

// update the whole state at once
this.todoStore.update(
  state => ({
    ...state,
    todos: [...state.todos, 'my first todo'],
  }),
  'add todo'
);

In combination with the snapshot functionality it would also be possible to update the state like this:

// manipulate the store by using latest values from snapshot
this.todoStore.updateProperty('todos', [
  ...this.todoStore.snapshot.todos,
  'new todo',
]);

Destroy Store

When a store service is not needed anymore it can be destroyed by calling the destroy method. This method ensures that no state changes are triggered to the subscribers of the store.

// store is not triggering events
this.todoStore.destroy();

Usage of Redux Devtools

To use the dev tools it is necessary to import the dev tools module:

import { StateDevToolsModule } from 'angular-reactive-state/dev-tools';

@Component({
  ...
  imports: [StateDevToolsModule],
  standalone: true,
})
export class AppComponent { ... }

or when there's a module

import { StateDevToolsModule } from 'angular-reactive-state/dev-tools';

@NgModule({
  imports: [StateDevToolsModule],
})
export class AppModule {}