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

mega-box

v1.0.3

Published

Easy, lightweight and powerful state management tool

Downloads

14

Readme

Mega Box

NPM

Easy, lightweight and powerful state management tool designed as redux or mobX replacement.

Install

npm i -s mega-box

Why megaBox?

  • No reducers and middleware hell for large projects
  • Simple usage and small api but smart optimisation inside
  • No boilerplate code
  • Easy to share store cross prjects or packages

Usage

megaBox(initialState: Object, plugins?: { [name]: (state: Object) => unknown })

Start with creating sate:

const initailState = { userName: 'Jack John', userId: 123 };
const appState = megaBox(initialState);

Plugins can be passed by second argument. Each plugin is function which get state as single argument.

const onUserIdChange = state => (cb: () => void) => {
    return state.subscribe(cb, ['userId']);
}

const appState = megaBox(initialState, { onUserIdChange });

appState.onUserIdChange(() => {
    console.log('UserId is changed and this handled by plugin');
});

Read value

To read value just get it from state as from object:

const user = appState.user;
// or
const { user} = appState;

Write single value

To write single value set it as in object:

appState.user = ‘John Smith’;

Write many values at one time

To update few fields in state call appState.put:

appState.put({ user: 'John Smith', userId: 987 });

Subscribe on changes

appState.subscribe(callback, filter?: string[] | false)

Avoid to writing more than one value by setting them in state. Use put instead.
Every single write in state will call callback separately.

callback - required. Will be called immediately after subscription and when state changes. Callback receive actual state as first argument and boolean as second argument. Boolean specify if it called first time (true) or after something change (false).

filter - optional. Used to describe which state keys should be changed to invoke callback. Possible values:

  • false - will disable filter at all and callback will be called every update
  • string[] - specify keys to track changes
  • Leave it empty to let megaBox automaticaly solve when call callback

MegaBox remember which values used in first subscriber callback call and optimize performance by calling callback only when those values are changed.

Use state distructurization and read required values at the start of callback function.
Avoid getting values in conditions, some conditions can be scipped at the first call and lead to bugs when listener don’t call when value is changed.

To avoid callback execution when some values changed use appState.<key> instead of getting value from listener params.

// Invoke callback every time when user is changed
appState.subscribe(({ user }) => {
  // Get value when the user changed but changing userId not invoke callback
  Const { userId } = appState;
});