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

redux-hub

v2.0.0

Published

Use this library to create redux state nodes so you can plug-in as many as you want behaving as if they were a single one.

Downloads

11

Readme

ReduxHub

Use this library to create redux state nodes so you can plug-in as many as you want behaving as if they were a single one.

Build Status NPM Version Chat on Gitter

Installation

ReduxHub is available as an NPM package. You can install ReduxHub in your project's directory as usual:

$ npm install redux-hub --save

Usage

ReduxHub


StateHub class api

ReduxHub provides you with a class to instanciate your hub node.

StateHub is a Generic class and can be configured in advance so TypeScript can provide the right information.

Generics

On creating an instance of StateHub you need to provide the following Generics:

  1. State - Provide your state interface.
  2. Dispatchers - Provide your dispatchers interface.
  3. HubModel - Provide the Hub model interface.
  4. Result - Empty object where the node builder will store the used methods.
import { StateHub } from 'redux-hub';

const stateHub = new StateHub<State, Dispatchers, Hub, {}>();

StateHub instance api

Once you have created the instance you will be provided with one method to setting up your hub node.

node

Node method gets only one argument, the scope name for your hub node.

import { StateHub } from 'redux-hub';

const stateHub = new StateHub<State, Dispatchers, Hub, {}>();

const node = stateHub.node('scope-name');

Node builder api

Once we have set the node the api will provide two methods, one to set configuration and another one to create the state node.

set

This method receives two arguments, the first is the name of the property to set up and the second is the object to configure it.

The name of the properties that can be configured are actions, reducers and state.

Actions and reducers are mandatory

State is optional if the initial state is an empty object

import { StateHub } from 'redux-hub';

const stateHub = new StateHub<State, Dispatchers, Hub, {}>();

stateHub
    .node('scope-name')
    .set('actions', {})
    .set('reducers', {})
    .set('state', {});

create

This method, once you have configured the state node, can be used to get the state node instance.

Creating a node without configuring actions and reducers first will throw an error.

import { StateHub } from 'redux-hub';

const stateHub = new StateHub<State, Dispatchers, Hub, {}>();

const stateNode = stateHub
                    .node('scope-name')
                    .set('actions', {})
                    .set('reducers', {})
                    .set('state', {}) // In this case this line could be skipped.
                    .create();

ReduxHub Node API

dispatchers

This object wraps the action creators and dispatches the action on executing it.

node.dispatchers.test();

getState

This method returns node state.

node.getState();

createSubscriber

This method creates a subscriber to changes in state.

Subscriber Configuration Examples

Listening to any change in global state If you don't pass any configuration object to the subscriber it will listening to any state change and the listener function will be executed.

const subscriber = node.createSubscriber();
subscriber(listener);

Listening to any change in ReduxHub Node state If you want to react to all the changes in your ReduxHub Node state you have to pass a function that will return the state.

The listener function will be executed only if the state changed since last time it was executed.

const subscriber = node.createSubscriber({
    stateSelector: (state) => state
});
subscriber(listener);

Listening to an specific change in ReduxHub Node state If you want to react to an specific change in your state you have to pass a function that will return the state you want to check.

The listener function will be executed only if the specific state changed since last time it was executed.

const subscriber = node.createSubscriber({
    stateSelector: (state) => state.test
});
subscriber(listener);

Executing some code before executing the listener function If you need to execute some code before executing the listener function you can provide this function.

This function receives an object with the previous state and the current state.

The function will be only executed if the previous state and the current one are different.

If a function is provided as a handler property, the listener must be executed from inside of this function.

const subscriber = node.createSubscriber({
    handler: (stateHolder, listener) => {
        console.log(state.previous);
        console.log(state.current);
        listener();
    }
});
subscriber(listener);

You can also combine both methods to execute the handler only when an specific state has changed.

API documentation

API Documentation can be generated executing npm run docs. The documentation generated can be found inside the docs folder.

Build the source

This library has been written using TypeScript. If you need to use it in your project but you are not working with TypeScript you can always to build the code using npm run build This command will lint your code, run the tests and *compile to TypeScript.

Contributing

This project is maintained by a community of developers. Contributions are welcome and appreciated. You can find ReduxHub on GitHub; feel free to start an issue or create a pull requests: https://github.com/tcorral/redux-hub

For more information, read the contribution guide.

License

Copyright (c) 2019 Tomas Corral. Copyright (c) 2019 ReduxHub Contributors. Licensed under the MIT License.