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

@tsrt/store

v2.0.0

Published

Typescript Reusable Tools: RxJs State Store

Downloads

22

Readme

TsRT: Store

npm version GitHub license Size Downloads

State management tool on top of great RxJS.

Usage

Note! This package uses RxJS, thus any rxjs operators and knowledge could and should be used to play with state.

Prepare State

It is necessary to provide State interfaces for better type safety and TS intellisense while using set and get methods.

interface IUser { id: number; name: string }
interface ITodo { id: number; title: string }
interface IState {
  user: IUser;
  todos: ITodo[];
}
Create store
import { Store } from '@tsrt/store';

export const store = new Store<IState>();
Listen for changes
import { store } from 'path/to/store';

store.get('user')
  .subscribe((user) => { /* Get notified here on any user updates */ })

store.get('user.name')
  .subscribe((userName) => { /* Get notified here on only user.name updates */ })

store.get('todos')
  .subscribe((todos) => { /* Get notified here on any todos updates */ })

store.get('todos.1')
  .subscribe((secondTodo) => { /* Get notified here on only second todo updates */ })

store.get('todos.1.title')
  .subscribe((secondTodoTitle) => { /* Get notified here on only second todo.title updates */ })
Selectors
store.get('user')
  .select((user) => ({ pk: user.id, firstName: user.name }))
  .subscribe((user) => { /* Get notified here on any user updates */ })

store.get('todos')
  .select((todo) => todo.id)
  .subscribe((todoIds) => { /* Get notified here on only todos updates, each value will be `number[]` (todo.id[]) */ })
RxJS usage examples
const todos$ = store.get('todos');
const todosTotal$ = todos$.pipe(map((val) => val.length));

todos$
  .pipe(
    withLatestFrom(todosTotal$)
  )
  .subscribe(([todos, total]) => { /* Get notified here on any todos updates. Receive todos and its total count */ })

todos$
  .pipe(
    map((val) => val.filter((todo) => todo.id % 2 !== 0)),
  )
  .subscribe((todos) => { /* Get notified here on any todos updates. Leave only todos with even id */ })

// And more ...
Provide test values
store.set('user', { id: 1, name: 'Me' });
store.set('todos', [{ id: 1, title: 'First Todo' }, { id: 2, title: 'Second Todo' }]);
store.get('todos.1.title').set('Updated Second Todo Title');
store.set('todos.1.title', 'Updated Again Second Todo Title');
Angular Example (optional)

See on Stackblitz

assign option

export interface IStoreOptions {
  /**
   * Whether to assign new object into existing in store or just replace it.
   *
   * @example
   * const user = new User({ id: 1, name: 'Me' });
   * const store = new Store({ user });
   *
   * // If `assign.object` === false
   * store.get('user').set({ name: 'You })
   * store.state.user // { name: 'You' }
   *
   * // If `assign.object` === true
   * store.get('user').set({ name: 'You })
   * store.state.user // User { id: 1, name: 'You' }
   *
   * @property [object] - Defines whether to assign when updating object values. @default false.
   * @property [array] - Defines whether to assign when updating array values. @default false.
   */
  assign?: {
      object?: boolean;
      array?: boolean;
  };

  /**
   * Wherther to use strict rule for deep equality via JSON.stringify for distinctUntilChanged by key
   * This will prevent extra event firing if Object link changed but actual value is the same.
   *
   * @default false.
   */
  strictDistinct?: boolean;
}

Supported versions

Version | Supported ------- | ----- 2.x | :white_check_mark: 1.x | :x: 0.x | :x:

Disclaimer

For quite long time there was quite frequent necessity to use some centralized store, but without big complexity and overhead, which is provided by popular modern tools, such as NgRX, NgXS, Redux, etc.

This is a bit enhanced revision of already used and existing Store, including some useful modern APIs and still tiny solution without any boilerplate.

License

This project is licensed under the terms of the MIT license.