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

weedux

v3.5.3-beta

Published

A toy redux like thing

Downloads

9

Readme

Weedux

Build Status

A tiny simple toy redux like thing for learning.

How to use

Install like so:

$ npm install weedux --save

Use it like this:

import weedux, { middleware } from 'weedux';
const { thunk } = middleware;

const initialState = { counter: 0 };

const reducers = [
  (currentState, action) => {
    const newState = currentState;

    if (action.type === "INCREMENT_COUNTER")
        newState.counter = currentState.counter + 1;

    return newState;
  },
  (currentState, action) => {
    const newState = currentState;

    if (action.type === "DECREMENT_COUNTER")
        newState.counter = currentState.counter - 1;

    return newState;
  },
];

/*

or you can do this:

const initialState = { ticker: { counter: 0 } };

const reducers = {
    ticker: (state, action) => {
        switch(action.type) {
            case 'INCREMENT_COUNTER':
            return {
                ...state,
                counter: state.counter + 1,
            };
            case 'DECREMENT_COUNTER':
            return {
                ...state,
                counter: state.counter - 1,
            };
            default: 
              return state;
        }

    }
};
*/

const store = new weedux(initialState, reducers, [thunk]);

store.subscribe((newState) => console.log("State Updated:", newState));

const dispatch = store.dispatch;

dispatch({type: "INCREMENT_COUNTER"});

// do async stuff using the thunk middleware
dispatch((dispatcher, store) => {
  dispatcher({type: "API_CALL_UPDATE_START"});

  fetch("/my/api/endpoint")
    .then((res) => res.json())
    .then((data) => dispatcher({ type: "API_CALL_UPDATE_SUCCESS", payload: data }))
    .catch(() => dispatcher({type: "API_CALL_UPDATE_FAIL"}));
});

new weedux(initialState, [reducer], [middleware]):

Creates a new store.

initialState is an object that will seed the Flux Store with a start state.

reducer is a function (or array of functions) that get passed a state object and the dispatched flux action. The returned value from these functions become the new state. If passed an undefined reducer, an identity function is used.

middlewares is a function (or array of functions) that take the form of (store) => (next) => (action); pretty much the same layout as a redux middleware.

Comes with middleware on the weedux.middleware, use it like so:

import Weedux, { middleware } from 'weedux';
const { thunk, logger } = middleware;

const store = new Weedux({}, reducers, [thunk, logger]);

const dispatch = store.dispatch;
dispatch((d, state) => {
  d({ type: "MY_ACTION" });
})

store.dispatch(action) => Promise:

A function used to dispatch actions to the store. Returns a promise that resolves when the dispatch is completed.

action: An object that will be passed to the reducer.

store.subscribe(cb) => subscribeHandle

Adds a callback to the store that is fired whenever a dispatched action fully completes.

cb: is a callback that is passed the latest version of the store's state and the action that was used to update it.

returns a handle function that when invoked removes the associated callback from the internal listener

store.getState() => [Object]

returns a copy of the full state of the store.

connect(mapStateToProps, mapDispatchToProps, store) => func(React.Component)

A handy connector that returns a function that injects props/state into the specified React.Component and manages lifecycle events for you.

bindActionCreators({ object }, dispatch) => { object}

Takes an object where each property is a function and returns a new object where each function is wrapped in a dispatch call, passing any arguments that function recieves to the original wrapped function. This is useful for converting many flux actions into a dispatchable wrapped version that is easier to use in your code.

eg.

import { bindActionCreators } from 'weedux'

const action = name => ({ type: 'HELLO_ACTION', payload: { name } })

const wrappedAction = bindActionCreators({ action }, dispatch);

// will dispatch the above action
wrappedAction.action('bob');

LICENSE

MIT