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

@cimacmillan/refunc

v1.0.3

Published

State container with functions

Downloads

9

Readme

Refunc

Redux-like state container with actions as functions

By Callum Macmillan

Quick start

Installing

npm install @cimacmillan/refunc

Usage

Refunc takes actions as functions, with payload as parameters

const emptyActions = {
    increment: () => {},
    addX: (x: number) => {}
}

Reducers are defined as their initial state and functions for transforming state based on actions

const reducer = {
    state: {
        count: 0
    },
    actions: {
        increment: (state: State) => ({...state, count: state.count + 1}),
        addX: (state: State, x: number) => ({...state, count: state.count + x}),
    }
}

The store is created by passing a reducer definition, and the empty actions

const store = new Store(
    reducer,
    emptyActions
);

The store is updated by calling actions

store.getActions().increment();
store.getActions().addX(10);
// store.getState() = { count: 11 }

The store can notify listeners of changes to the state

store.addChangeListener(() => {
    console.log(`new state is ${store.getState()}`)
});

The stores actions can be subscribed to

store.subscribe({
    increment: () => {
        console.log("increment was called");
    },
    addX: (x: number) => {
        console.log(`addX was called with ${x}`);
    }
})

Multiple reducers can be combined with combineReducers

const countReducer: Reducer<CountState, Actions> = {
    state: 0,
    actions: {
        increment: (state: CountState) => (state + 1),
        addX: (state: CountState, x: number) => (state + x),
    }
}

const numberOfCallsReducer: Reducer<CallRecordState, Actions> = {
    state: 0,
    actions: {
        increment: (state: CountState) => (state + 1),
        addX: (state: CountState, x: number) => (state + 1),
    }
}

const store = new Store(
    combineReducers({
        count: countReducer,
        numberOfCalls: numberOfCallsReducer
    }),
    emptyActions
);

// store.getState() = { count: 0, numberOfCalls: 0 }

store.getActions().increment();

// store.getState() = { count: 1, numberOfCalls: 1 }

store.getActions().addX(10);

// store.getState() = { count: 11, numberOfCalls: 2 }

This can be used with React hooks with these effects

export const useGlobalState: () => [State, Actions] = () => {
    const [storeState, setStore] = React.useState(store.getState());
    React.useEffect(() => {
        const callback = () => setStore(store.getState());
        store.addChangeListener(callback);
        return () => store.removeChangeListener(callback);
    }, []);
    return [storeState, store.getActions()];
};

export const useDispatchListener = (actions: Partial<Actions>, deps: any[]) => {
    React.useEffect(() => {
        store.subscribe(actions);
        return () => store.unsubscribe(actions);
    }, deps);
}