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

simpleton-state-manager

v1.1.9

Published

A Simple State Manager for WebApp UI

Downloads

42

Readme

Simpleton State Manager

A super simple State Manager for your modern Web Application. In fact, it's so simple, that simple is in its name.

Whether you use an MVC framework such as React, Angular, Vue, WebComponents or plain Vanilla JavaScript, this universal, lightweight solution is all you need to manage your Application's Frontend state, no matter how complex it gets.

For any questions, comments or complaints: [email protected]

The Fundamentals

This State Manager is a plain JavaScript class which follows the Singleton Design Pattern. There is ever only a single instance of the class that is constructed per a browser window context. Every invocation of the constructor returns the very same object reference.

The Models are stored in the class's private member (identified by the "#" prefix) that is protected from the outside code and is accessed exclusively through a Proxy object.

Once you instantiate a SimpletonStateManager you can use it to store an almost limitless amount of individual Models, retrieve them as needed and subscribe to their changes.

You can even store functions as models.

Instantiating a SimpletonStateManager

  const store = new SimpletonStateManager(); //everyone gets the same static instance

Setting a Model

Models are any JavaScript Entity (Object, Array, Function or primitive) Whether you construct a new Model, or obtain it from an API call or by any other means you use the SimpletonStateManager to register this Model with a unique name. Be advised, there is no "updateModel" operation. Registering a new Model with the same name will overwrite the existing Model and notify all subscribers.

  const modelOne = { name: 'modelOne', value: 3 };
  store.setModel('modelOne', modelOne);

Getting a Model

Use the SimpletonStateManager instance to explicitly obtain a clone of the stored Model. The stored models are immutable, and you are never getting the actual reference:

  const sampleModel = store.getModel('modelOne');
  //do whatever you want to do with this model like render the UI

Subscribing to Model Changes

To be notified when a model is created or modified, you subscribe to a model by it's name, provide a unique subscriber name and a callback function.

A subscribrerKey is returned from the subscribe function. You need to retain this key so that you can use it to unsubscribe.

  const modelName = 'modelOne';

  const key = store.subscribe(modelName, (model) => { 
    console.log('Model changed', modelName, subscriberName, model);
    //do whatever you want to do with this model like render the UI
  }));

Notifying Subscribers

If for some reason (like a refresh) you want to explicitly notify all the Subscribers of a particular model:

  store.notifyModelListeners('modelOne'); //all the Model's subscribers will be notified

Unsubscribing

As with any event listeners, you should clean up your models and subscribers when they are no longer needed or in scope. This is typically done when the components is removed from the DOM (dismounts), or you have an internal navigation event. Complete page reloads will always result in a new SimpletonStateManager instance.

You can unsubscribe by name:

  store.unsubscribe(modelName, key); //the key you received from subscribe()

Or brute force to purge all data:

  store.unsubscribeAll();

Getting All Models

A handy convenience function to help you debug your state:

  const models = store.getModelList();
  console.log('All the Models', models);

Installation

This repo is now published to NPM.

For the simplest use case all you need to do is to copy the SimpletonStatManager.js file directly into your project's folder and import it using relative path

npm install simpleton-state-manager

React Hooks

(Please see examples!) useStateStore - this is the basic hook and does nothing more than returning the instance

  const store = useStateStore();

useStateToStoreBinding - this is a more advanced hook that wraps the Model subscriptions and bi-directionally binds it to a local useState variable. When the underlying Model changes, the State variable does as well and vice-versa. The Component is then re-rendered based on the new state. You can provide an optional default value (second arg) if the model has not yet been created, and an optional callback function in case you need to perform some operation besides binding the state.

This hook also automatically unsubscribes when the component is dismounted.

  const [something, setSomething] = useStateToStoreBinding('MODELNAME', [], callback);

Examples

Switch to the /examples folder and view the README file and run npm install, then npm start ,on any examples folder.

webcomponents_node

Example of a vanilla JS / WebComponents implementation running on Node.JS.

React_hooks

A React example which wraps the SimpletonStateManager in a Hook and is geared to demonstrate how to optimize the rendering of Components based on very targeted Model changes, not brute force.

An Interval starts and every 5 seconds will change a random box's color. Only the affected box's render counter will increment.

Click on a Box to randomly change its color while only re-rendering the box itself (the render-counter will only increment for the clicked box).

Click on the title to change a random box's color and re-render all the boxes.

Enjoy a Redux-free life!