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

bibux

v1.1.34

Published

An elegant React state management library

Downloads

88

Readme

DISCLAIMER

This project is still a work in progress and in early development.

BIBUX

Bibux, pronounced /bibyks/ (or "beebewx" in English), is an elegant React state management library with strong typing and a simple API.

Documentation

You can follow this README to get started quickly, or read the full documentation on bibux.org.

Setup

Install Bibux:

npm install bibux

Then, define your createStore using the init function:

import { init } from "bibux";

export const { createStore } = init();

You're ready to go!

Usage

Simply create a store with createStore:

export const counterStore = createStore({
    state: {
        counter: 0,
    },
    actions: {
        incr: s => s.counter++,
        decr: s => s.counter--,
        add: (s, n: number) => {
            s.counter += n;
        },
    },
});

Then use it directly in your components:

import { counterStore } from "./store";

const Counter = () => {
    const { counter } = counterStore.useState();
    const { incr, decr, add } = counterStore.useActions();
    return <div>
        {counter}
        <button onClick={incr}>+</button>
        <button onClick={decr}>-</button>
        <button onClick={() => add(10)}>+10</button>
    </div>;
};

Getters

If you need to compute values from your state, you can use getters:

export const counterStore = createStore({
    state: {
        counter: 0,
    },
    getters: {
        double: s => s.counter * 2,
    },
});

Getters are directly available in store.useState():

const { counter, double } = counterStore.useState();

Transitions

If you need to perform asynchronous operations, you can use transitions:

const authStore = createStore({
    state: {
        loading: false,
        username: undefined as string | undefined,
    },
    getters: {
        connected: s => s.username !== undefined,
    },
    actions: {
        logout: s => {
            s.username = undefined;
        },
    },
    transitions: {
        login: async ({ set, get }, username: string, password: string) => {
            if (get().loading) throw new Error("Already loading...");
            set(s => s.loading = true);
            await API.login(username, password);
            set(s => {
                s.username = username;
                s.loading = false;
            });
            console.log("Connected as", get().username);
        },
    },
});

Transitions are available in store.useTransitions():

const Demo = function () {
    const { connected, username, loading } = authStore.useState();
    const { logout } = authStore.useActions();
    const { login } = authStore.useTransitions();
    if (loading)
        return <div>Loading...</div>;
    if (connected)
        return <div>
            Connected as {username}
            <button onClick={logout}>
                Logout
            </button>
        </div>;
    return <div>
        <button onClick={() => {
            login("user", "pass")
                .then(() => console.log("logged in"))
                .catch(console.error);
        }}>
            Login
        </button>
    </div>;
}

State mutations

Internally, Bibux uses immer to handle state mutations. This means that you can mutate your state directly in your actions and transitions, and Bibux will take care of creating a new immutable state for you.

Middlewares

You can use middlewares to intercept actions and transitions:

Simply register your middlewares when calling init:

import { init } from "bibux";

export const { createStore } = init({
    middlewares: [
        // ...
    ],
});

To create your own middleware, simply use the createMiddleware function:

import { createMiddleware } from "bibux";

const logger = createMiddleware({
    action: ({ name, args, run }) => {
        console.log("Before action", name, args);
        run();
        console.log("After action", name, args);
    },
    transition: async ({ name, args, run }) => {
        console.log("Before transition", name, args);
        await run();
        console.log("After transition", name, args);
    },
});

Popular middlewares

Store enhancers

WIP

Popular store enhancers

Difference between middlewares and store enhancers

Basically, both middlewares and store enhancers serve the same purpose: they allow you to intercept actions and transitions. The main difference is that middlewares are applied to every store, while store enhancers are applied to a specific store.