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

@elie29/store

v2.0.0

Published

A simple store management for front-end applications

Downloads

26

Readme

Store Management

Build Status Coverage Status Open Source Love

Frontend Application Store

A simple frontend store that manages application state using RxJS BehaviorSubject. The main purpose is to provide a straightforward, simple and agnostic library for any frontend application where sharing data among services, modules or containers is desired.

By default, the store uses a shallow clone version of the state. However, we can provide another cloning strategy (eg. lodash cloneDeep) so the store would treat the state immutably, and any data manipulation outside the store, would not affect the store at all.

The image below illustrates the appropriate one-way dataflow implied by the store:

Store Management

What is a state?

In its easiest way, a state is a snapshot of an application data at a specific time. Whenever data is manipulated or changed, a new state is created and saved in the store. In our case, the state is represented by extending State interface as follow:

interface BasicState extends State {
  author?: Author;
  loading: boolean;
  posts: Post[];
}

An initial state is a simple object that implements BasicState as follow:

const INITIAL_STATE: BasicState = {
  author: undefined,
  loading: false,
  posts: []
}

Why immutability is important?

  • Single Source of Truth: The store is solely responsible of handling data.
  • State is read-only: Modifying the state outside the store does not affect the store.
  • One-way dataflow: A new state is published to subscribers any time the store receives data.
  • Predictable: The state evolution can be tracked to figure out how and who made the changes.

Install and Configure the Store

To get started with the store, we have two options. We can either download the latest release or run npm install:

Or:

  • Run npm install @elie29/store

Then:

  • Optionally run
    • npm install rxjs: If it is not installed already
    • npm install lodash: If you want to use cloneDeep and not installed already

Once the store dependencies installed, we need to:

  1. Define application state (cf. BasicState)
  2. Create initial state (cf. INITIAL_STATE)
  3. Extend abstract store class as follow:
export class BasicStore extends Store<BasicState> {
  constructor() {
    super(INITIAL_STATE);
  }
}

Settings

By default, we don't log state changes and we use a shallow clone strategy function. We can change these settings by providing a StoreSettings to the constructor as follow:

export class BasicStore extends Store<BasicState> {
  constructor() {
    super(INITIAL_STATE, {
      logChanges: true,
      cloneStrategy: cloneDeep // or other techniques
    });
  }
}

Now BasicStore could be injected in any service or container. It is also possible to :

  1. Create an instance of the store in order to be shared across the application.
  2. Create for each module its own instance of store.

Store instances are isolated and DOES NOT share any data between them.

Store Management in Angular Service

@Injectable({
  providedIn: 'root' // or in a module specific providers
})
export class BasicStore extends Store<BasicState> {
  constructor() {
    super(INITIAL_STATE);
  }
}

Store API

The store API is very simple and contains few public methods:

  1. value: A getter for the current cloned state.
  2. get: Retrieves a specific key from the state: eg. get('author') or get('loading').
  3. set: Updates a specific state key in the store: eg. set('loading', true).
  4. patch: Updates the state or a slice of the state.
  5. select: Watches for a value change of a specific key in the store. It returns an observable of read-only data: eg. select('author').subscribe(next => console.log(next)).
  6. watch: Watches and keeps track on store changes.

N.B.: By default, data passed or retrieved from the store is NOT deep cloned. So any manipulation of data DOES affect the store unless we implement lodash cloneDeep function which is highly recommended.

Peer Dependencies

The store management library depends on:

  1. RxJS ^7.0

    • BehaviorSubject, Observable
    • distinctUntilChanged, map, pluck
  2. lodash [recommended but not required]

    • cloneDeep
  3. Typescript 4.9.5

    • store tested with angular

Publish to npm repo

  1. Fix date release in CHANGELOG.md
  2. Increment version number in package.json and package-lock.json
  3. Run npm run pub then enter the 2FA code
  4. Commit, push and create a new github release