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

@veams/status-quo

v0.0.2

Published

The manager to rule states in frontend.

Downloads

4

Readme

Status Quo (@veams/status-quo)

The Manager to rule your state.


Table of Content

  1. Getting Started
  2. Example

Getting Started

  1. Create your own state handler which handles all the streams and a state you expose next to the actions
  2. Use actions and state in your component
  3. When using React, initialize the state handler with a custom hook called useStateFactory() (or useStateSingleton() for Singleton states)

These three steps are necessary to create a completely decoupled state management solution without the need of creating custom hooks with useEffect().

Note: Please keep in mind that dependencies for the hook needs to be flattened and cannot be used as an object due to how React works.

Example

Let's start with a simple state example. You should start with the abstract class BaseState:

import { useStateFactory, StateHandler } from '@veams/status-quo';

type CounterState = {
  count: number;
};
type CounterActions = {
  increase: () => void;
  decrease: () => void;
};

class CounterStateHandler extends StateHandler<CounterState, CounterActions> {
  constructor([startCount = 0]) {
    super({ initialState: { count: startCount } });
  }
  
  getActions() {
    return {
      increase() {
        this.setState({
          count: this.getState() + 1
        })
      },
      decrease() {
        const currentState = this.getState();

        if (currentState.count > 0) {
          this.setState({
            count: currentState - 1
          })
        }
      }
    }
  }
}

export function CounterStateFactory(...args) {
  return new CounterStateHandler(...args);
}

This can be used in our factory hook function:

import { useStateFactory } from '@veams/status-quo';
import { CounterStateFactory } from './counter.state.js';

const Counter = () => {
  const [state, actions] = useStateFactory(CounterStateFactory, [0]);
  
  return (
    <div>
      <h2>Counter: {state}</h2>
      <button onClick={actions.increase}>Increase</button>
      <button onClick={actions.decrease}>Decrease</button>
    </div>
  )
}

What about singletons?

Therefore, you can use a simple singleton class or use makeStateSingleton() and pass it later on to the singleton hook function:

import { makeStateSingleton } from '@veams/status-quo';

import { CounterStateHandler } from './counter.state.js';

export const CounterStateManager = makeStateSingleton(() => new CounterStateHandler([0]))
import { useStateSingleton } from '@veams/status-quo';
import { CounterStateManager } from './counter.singleton.js';

const GlobalCounterHandler = () => {
  const [_, actions] = useStateSingleton(CounterStateManager);
  
  return (
    <div>
      <button onClick={actions.increase}>Increase</button>
      <button onClick={actions.decrease}>Decrease</button>
    </div>
  )
}

const GlobalCounterDisplay = () => {
  const [state] = useStateSingleton(CounterStateManager);
  
  return (
    <div>
      <h2>Counter: {state}</h2>
    </div>
  )
}