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

arachnoid

v0.7.0

Published

<p align="center"> <a href="" rel="noopener"> <img width="200px" height="200px" src="arachnoid.jpg" alt="Project logo" /></a> </p>

Downloads

6

Readme

Status GitHub Issues GitHub Pull Requests License


📝 Table of Contents

🧐 About

🏁 Getting Started

Prerequisites

You should have react setup and running

Using vite

npm create vite

Using CRA

npm create react-app

Installing

Install Arachnoid using npm

npm install arachnoid

Or Yarn

yarn add arachnoid

🎈 Usage

First create a store

import { createStore } from "arachnoid"

const useCountStore = createStore({
    state: {
        count: 0,
    },

    actions: {
        increment: (get, set) => {
            set((state) => ({
                ...state,
                count: state.count + 1,
            })
            )
        }
    }
});

Then bind your components, and that's it!!

Use the hook anywhere, no providers are needed. Dispatch the actions and et'voila! The component will re-render on the changes.

const Test = () => {

    const instance1 = useCountStore();
    return (
        <h1 onClick={() => instance1.dispatch('increment')}>
            Hello {instance1.getState().count}
        </h1>
    )
}

Dealing with asynchronous functions

To use deal with asynchronous function, we can use the function createAsyncAction and use it along with regular archanoid actions.

import { createStore, createAsyncAction } from "arachnoid";

const useAsyncStateStore = createStore({
    state: {
        todo: [],
    },

    actions: {
        asyncFetch: (get, set, { num }) => createAsyncAction(async ({ num }) => {
            const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${num}`);
            return await res.json();
        }, (asyncResponse) => {
            const { data, isLoading, isError } = asyncResponse;

            if (!isLoading && !isError) {
                set(state => ({
                    todo: [...state.todo, ...data],
                }))
            }
        }, { num })
    }
})

The data returned by the async function can be accessed from asyncResponse inside the callback function. We can pass any additional require as a payload while dispatching.

We can then dispatch this asynchronous action like we always do!!

const Test = () => {

    const instance1 = useAsyncStateStore();
    return (<>
        <h1 onClick={() => instance1.dispatch('asyncFetch')}>
            Add Todo
        </h1>
        <ol>
            {
                instance1.getState().todo.map(ele=>(<li>ele.title</li>));
            }
        </ol>
    </>)
}

Using Middlewares

Arachnoid provides bare-bones middleware support for its stores using createArachnoidMiddleware function.

import { createStore, createArachnoidMiddleware } from "arachnoid";

const middleware1 = createArachnoidMiddleware((get, set, action)=>{
    console.log (`${action.name} has been called with payload ${JSON.stringify(action.payload)}`);
})

export const store = createStore({
    state: {
        count: 0,
    },

    actions: {
        increment: (get, set) => {
            set((state) => ({
                ...state,
                count: state.count + 1,
            })
            )
        }
    }
}, [middleware1]);

We can prevent certain actions from using middlewares by passsing ignore actions array to createArrachnoidMiddleware.

import { createStore, createArachnoidMiddleware } from "arachnoid";

const middleware1 = createArachnoidMiddleware((get, set, action)=>{
    console.log (`${action.name} has been called with payload ${JSON.stringify(action.payload)}`);
}, ["decrement"])

export const store = createStore({
    state: {
        count: 0,
    },

    actions: {
        increment: (get, set) => {
            set((state) => ({
                ...state,
                count: state.count + 1,
            })
            )
        },
        decrement: (get, set) => {
            set((state) => ({
                ...state,
                count: state.count - 1,
            })
            )
        }
    }
}, [middleware1]);

Here all the actions except decrement will execute the middleware1.

Adding Listeners

Arachnoid providers bare-bones support for listeners subscribing to the state changes.

We can define listeners while creating the store.

const store = createStore({
    state: {
        count: 0,
    },

    actions: {
        increment: (get, set) => {
            set({
                ...get(),
                count: get().count + 1,
        })
        }
    },
    listeners: {
        test: (get)=>{
            console.table(get())
        }
    }
});

In here the listener test will listen to all the state changes, and execute itself then.

OR

We can also subscribe or unsubscribe to a listener inside a component.

const Test = () => {

   const instance1 = store();

   useEffect(()=>{
       instance.subscribe("test2", get=>console.log(get()))
       instance.unsubscribe('test');
   }, [])

   const handleClick = () => instance2.dispatch('increment')
   return (
       <h1 onClick={handleClick}>
           Hello {instace1.getState().count} {instance2.getState().count}
       </h1>
   )
}

⛏️ Built Using

✍️ Authors

See also the list of contributors who participated in this project.

🎉 Acknowledgements

  • Hat tip to anyone whose code was used
  • Inspiration
  • References