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-simple-signal-store

v1.0.5

Published

A simple way to create signal stores with a read-only interface.

Downloads

7

Readme

NgxSimpleSignalStore

A simple way to create signal stores with a read-only interface.

Compatibility with Angular Versions

Table of contents

Installation

npm ngx-simple-signal-store
# Or if you use yarn
yarn add ngx-simple-signal-store

Usage

Create global store

Add the store provider with an initial state and a unique token to your app.config.ts as a provider:

import { createInjectionToken, provideStore } from 'ngx-simple-signal-store';

export interface DemoState {
  theAnswerToLife: number;
}

export const initialDemoState: DemoState = {
  theAnswerToLife: 42
};

export const demoStateToken = createInjectionToken<DemoState>();

export const appConfig: ApplicationConfig = {
  providers: [
    provideStore(initialDemoState, demoStateToken),
  ],
};

Then, import and inject it into your components:

import { demoStateToken } from './app.config.ts';

@Component({})
export class DemoComponent {
  readonly #demoStore = inject(demoStateToken);
}

Create component store

Add the store provider with an initial state and a unique token to your component as a provider:

import { createInjectionToken, provideStore } from 'ngx-simple-signal-store';

export interface DemoComponentState {
  theAnswerToEverything: number;
}

export const initialDemoComponentState: DemoComponentState = {
  theAnswerToEverything: 42
};

export const demoComponentStateToken = createInjectionToken<DemoComponentState>();

@Component({
  providers: [provideStore(initialDemoComponentState, demoComponentStateToken)]
})
export class DemoComponent {
  readonly #demoComponentStore = inject(demoComponentStateToken);
}

API

T is the type of the state.

state: { [K in keyof T]: Signal<T[K]> };

const cookieExists: boolean = demoStore.state;

Return with the readonly state. The returned object keys are the referenced state keys, and the values are the read-only signals.

setState(key: K, data: T[K]): void;

demoStore.setState('key', 0);

Sets the state with a specified key and value.

patchState(key: K, data: T[K] | Partial<T[K]>): void;

// primitive:
demoStore.patchState('key', 0);
// The value before patch: 1
// The value after patch:  0

// array:
demoStore.patchState('key', [3]);
// The value before patch: [1, 2]
// The value after patch:  [1, 2, 3]

// object:
demoStore.patchState('key', {value: 0});
// The value before patch: {value: 1}
// The value after patch:  {value: 0}

Patch the state with a specified key and value.

A callback function can be used for the complex operations:

demoStore.patchState('key', state => ({value: state.value + 1}));

resetStore(): void;

demoStore.resetStore();

Reset the store to the initial state.