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-signal-storage

v0.1.2

Published

Angular Signal Storage

Downloads

73

Readme

NgxSignalStorage

Usage Example

Provide to AppConfig

  bootstrapApplication(AppComponent, {
    providers: [
      ...other providers,
      provideNgxSignalStorage(),
    ],
  });

With prefix

If needed, you can use a prefix to prevent conflicts between keys in the storage. Example:

  bootstrapApplication(AppComponent, {
    providers: [
      ...other providers,
      provideNgxSignalStorage('<your-storage-key-prefix>'),
    ],
  });

With a prefix provided, all LocalStorage keys set through NgxSignalStorage will automatically have the prefix added.

NgxSignalStorageService APIs

Inject Service

  type StorageState = { todos: string[] };

  storage = inject(NgxSignalStorageService<StorageState>);

Use the inject() function provided by Angular to get the service instance. The NgxSignalStorageService accepts a generic type, which can be used as a type reference for the service APIs.

(Note: The service APIs themselves do NOT take a generic type.)

Getting value from LocalStorage (once)

  todos = this.storage.get('todos');

the get() method returns a readonly signal of the value retrieve from LocalStorage using the key argument. If a type is provided to NgxSignalStorageService, the value will be typed as the type provided (eg. string[] in the example) or null.

Watching value from LocalStorage (reactively)

  todos = this.storage.watch('todos');

Similar to get(), watch() also returns a readonly signal of the value retrieved from LocalStorage using the key argument. If a type is provided to NgxSignalStorageService, the value will be typed as the provided type (e.g., string[] in the example) or null. The difference is that the signal returned from watch() will react to value changes using the set(), remove(), and clear() method from NgxSignalStorageService.

Setting value to LocalStorage

  const todos = ['Laundry', 'Groceries'];

  this.storage.set('todos', todos);

Similar to the setItem() method from LocalStorage, set() takes in the key and value to set the value in LocalStorage. However, the difference is that the set() method from NgxSignalStorageService will trigger a change, and the readonly signals returned by watch() and has() will reactively get updated whenever set() is called.

Removing value from LocalStorage

  this.storage.remove('todos');

Similar to the removeItem() method from LocalStorage, remove() will use the key argument to remove the value associated with the key from LocalStorage. The difference is that this will trigger the readonly signals created using watch() and has() to react to this removal, which should result in those signals emitting null since the value will no longer exist in the LocalStorage.

Clearing all values from LocalStorage

  this.storage.clear();

Similar to the clear() method from LocalStorage, clear() will clear the entire LocalStorage. But similar to other methods from NgxSignalStorageService, it will trigger changes reactively, resulting in all readonly signals emit null.

Checking if a key exists in LocalStorage (reactively)

  hasTodos = this.storage.has('todos');

Similar to watch(), the has() method takes a key and returns a readonly signal, which will react to any changes in the LocalStorage through NgxSignalStorageService. The difference from watch() is that has() returns a signal of type boolean. If a key is set and exists in LocalStorage through NgxSignalStorageService, the signal will emit true; otherwise, it will emit false.

Watching any change from the service

  change = this.storage.change;

  this.change();

The change signal is readonly; it updates whenever any change goes through NgxSignalStorageService. It emits different actions going through NgxSignalStorageService. Refer to below for the types of the actions emitted:

  type GetStorageAction<Key> = { type: 'get'; key: Key };
  type SetStorageAction<Key, Value> = {
    type: 'set';
    key: Key;
    payload: Value;
  };
  type RemoveStorageAction<Key> = { type: 'remove'; key: Key };
  type InitStorageAction = { type: 'init' };
  type ClearStorageAction = { type: 'clear' };

Validation

  todos = this.storage.get('todos', (value) => Array.isArray(value));

When working with NgxSignalStorageService, you have the option to include a validator as a callback function when using the get() and watch() methods. This validator function should return true if the value is valid, and false if it's not. If the validator returns false, the get() and watch() methods will raise an Error. This helps ensure that the value retrieved is of the correct type.

(Note: This library is written using Angular 16, but theoretically, it should work with higher versions of Angular without any issues. Please file an issue on the repo if you encounter any problems while using it.)