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

@mcastiello/simple-store

v0.0.3

Published

A simple state manager that can store data via a reducer function.

Downloads

20

Readme

Simple Store

A simple state manager that can store data via a reducer function.

Install

yarn add @mcastiello/simple-store

npm install @mcastiello/simple-store

Create a store

In order to create a store, you will need to provide a reducer function to allow the users to modify the state.

While it is not essential, it is recommended to provide type definitions for your state and all the actions supported. This will allow the store manager to provide a safer environment and a better developer experience (your IDE will be able to suggest you the type of the payload of your actions, based on the action type you are using).

The following is a simple example of how to define your store.

import { ActionDefinition, Reducer, StoreInterceptor,  createStore } from "@mcastiello/simple-store";

// Define the list of actions supported by your store
enum Action {
  Initialise,
  Update,
  Destroy,
}

// Define the payload of each action
type Definitions = {
  [Action.Initialise]: ActionDefinition<Action.Initialise, boolean>;
  [Action.Update]: ActionDefinition<Action.Update, string>;
  [Action.Destroy]: ActionDefinition<Action.Destroy, undefined>;
};

// Define the type of your state
type State = {
  init?: boolean;
  value?: string;
};

// Create the reducer function
const reducer: Reducer<State, Definitions> = (state = {}, action) => {
  switch (action.type) {
    case Action.Initialise:
      return { ...state, init: action.payload };
    case Action.Update:
      return { ...state, value: action.payload };
    case Action.Destroy:
      return {};
    }
};

// Initialise your store
const store = createStore(reducer);

dispatch

The dispatch method allows you to send action that will eventually update the state

store.dispatch(Action.Initialise, true);

If you defined the types correctly, TypeScript will throw a type error if you try to dispatch the wrong payload for the chosen action.

subscribe

The subscribe method allows you to register a function that will be executed asynchronously whenever the state gets updated.

store.subscribe((state) => console.log("The state has been updated", state));

It is possible to lock your subscription to a set of specific actions. If you do so, your subscription will only be executed when one of the provided actions is dispatched.

store.subscribe((state) => console.log(`The updated value is ${state.value}`), [Action.Update]);

Your subscription will also receive the action that caused the state to update.

store.subscribe((state, action) => action && console.log(`The state has been updated by the action ${action.type}`));

The action parameter could be undefined, this is because, when registered, each subscription is immediately executed with the current value of the state.

intercept

The intercept method allow you to intercept an action and modify the new state created by the reducer. Interceptors need to be associated with one or more actions, and they will always need to return a version of the state.

The following is a simple example of a use case for using an interceptor. The user is storing a value, and the interceptor is determining if the value has gone up or down.

import { ActionDefinition, Reducer, StoreInterceptor,  createStore } from "@mcastiello/simple-store";

// Define the list of actions supported by your store
enum Action {
  SetValue,
}

enum Direction {
  Up,
  Down,
  Stable
}

// Define the payload of each action
type Definitions = {
  [Action.SetValue]: ActionDefinition<Action.Initialise, boolean>;
};

// Define the type of your state
type State = {
  value: number;
  direction: Direction;
};

const initialState: State = {
  value: 0,
  direction: Direction.Stable,
};

// Create the reducer function
const reducer: Reducer<State, Definitions> = (state, action) => {
  switch (action.type) {
    case Action.SetValue:
      return { ...state, value: action.payload };
  }
};

// Initialise your store
const store = createStore(reducer, initialState);

// Intercept the state and compare it with the previous version to update the direction property accordingly
store.intercept((oldState, newState) => {
  if (newState.value > oldState.value && newState.direction !== Direction.Up) {
    return { ...newState, direction: Direction.Up };
  } else if (newState.value < oldState.value && newState.direction !== Direction.Down) {
    return { ...newState, direction: Direction.Down };
  } else if (newState.value === oldState.value && newState.direction !== Direction.Stable) {
    return { ...newState, direction: Direction.Stable };
  }
  return newState;
}, [Action.SetValue])

Basically, you can use the interceptors to extend the functionality of the reducer, as they are executed after the reducer synchronously, and their result is eventually sent tot the subscriptions.

Your function will receive the action that updated the state as a third parameter. If your interceptor is meant to handle more than one action, you can use that to identify how to update the state.

store.intercept((oldState, newState, action) => {
  switch (action.type) {
    // Your action based logic
  }
}, [Action.Initialise, Action.SetValue]);

state

The store object will come with a state property that will allow you to access the current values stored in the state. The property is just a proxy that will only let you read the content, not modify it.

console.log(store.state.value);

The library is not freezing the content of the state (mostly for performance reasons), so while you won't be able to update primitive values of the state, you will still be able to insert values in arrays, or update the content of child objects. Doing so would of course defy the purpose of a reducer, so, do it at your own risk (and TypeScript may still throw a type error in some scenarios).