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-stateful-service

v0.1.3

Published

A lightweight state management for Angular

Downloads

12

Readme

Stateful Service :sparkles:

Lightweight state management for Angular.

Github repository

Compatibility with Angular Versions

Angular version 14.0.0 or higher is required.

Table of contents

Installation

npm i ngx-stateful-service

Initial setup

Initializing state with modules approach

With the standard modules approach, all you have to do is import the StatefulServiceModule and provide it with an initial state:

import { StatefulServiceModule } from "ngx-stateful-service";

interface ExampleState {
  firstName: string;
  todos: string[];
}

@NgModule({
  declarations: [ExampleComponent],
  imports: [
    CommonModule,
    StatefulServiceModule.withConfig<ExampleState>({
      initialState: {
        firstName: "Maciej",
        todos: ["Vacuum the apartment"],
      },
    }),
  ],
  providers: [],
})
export class ExampleModule {}

Initializing state with standalone components approach

The initialization of state in the standalone components approach resembles that of the module approach. However, in this case, you'll utilize the importProvidersFrom method to provide the StatefulServiceModule:

interface ExampleState {
  firstName: string;
  todos: string[];
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    CommonModule,
    RouterOutlet,
    RouterModule.forRoot([
      {
        path: "",
        loadComponent: () => import("./example/example.component").then((m) => m.ExampleComponent),
        providers: [
          importProvidersFrom(
            StatefulServiceModule.withConfig<ExampleState>({
              initialState: {
                firstName: "Maciej",
                todos: ["Vacuum the apartment"],
              },
            })
          ),
        ],
      },
    ]),
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Accessing the StatefulService

In components declared within the proper module/route, you have access to the previously initialized instance of StatefulService:

import { StatefulService } from "ngx-stateful-service";

@Component({
  selector: "app-example",
  templateUrl: "./example.component.html",
})
export class ExampleComponent {
  private readonly exampleStatefulService = inject(StatefulService<ExampleState>);
}

Creating custom stateful service

You can also create a custom stateful service that includes all the features of the default stateful service. To initialize an initial state within it, you should pass an object as an argument in the constructor:

import { CustomStatefulService } from "ngx-stateful-service";

@Injectable()
export class ExampleStatefulService extends CustomStatefulService<ExampleState> {
  constructor() {
    super({
      firstName: "Maciej",
      todos: ["Vacuum the apartment"],
    });
  }
}

State manipulation

Extracting state

The Stateful Service offers several methods through which you can extract data:

this.exampleStatefulService.getStateSlice$("todos"); // observable of ['Vacuum the apartment']

this.exampleStatefulService.getStateSliceValue("todos"); // ['Vacuum the apartment']

this.exampleStatefulService.getWholeState$(); // observable of {name: 'Maciej', todos: ['Vacuum the apartment']}

this.exampleStatefulService.getWholeStateValue(); // {name: 'Maciej', todos: ['Vacuum the apartment']}

Updating state

To update data, you can use the patchState method, which accepts a Partial of the declared state interface:

this.exampleStatefulService.patchState({
  todos: [...this.exampleStatefulService.getStateSliceValue("todos"), "Cook dinner"],
}); // adds 'Cook dinner' to the todos array

Reseting state

Last but not least, you can use resetWholeState and resetStateSlice to reset the state to its initial data:

this.exampleStatefulService.resetWholeState(); // sets whole state to initial value

this.exampleStatefulService.resetStateSlice("todos"); // sets todos property to its initial value

Example

Here you can find demo component where an example of use is presented.