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

rehoard

v0.1.9

Published

ReHoard is a pub/sub state management tool for ReactJs or Nodejs

Downloads

106

Readme

REHOARD

Introduction

Rehoard (simply pub/sub) is a project built in 2015 as an alternative to redux for internal use only (never meant to be used by others, so bug and untested parts might exists).

It is a simply pub/sub for ReactJs, Vanilla JS and NodeJs. It support local/session storage with redo/undo of actions.

Some features

  • Web browser, it will save everything by default using sessionStorage in case of accidental web refresh. (check settings, for changing from sessionStorage to localstorage or off)
  • Allows redo/undo up to 100 values. You may increase the size and should not affect performance. Note: this is only for state value and not the subcribers. Any subscriber which is not longer subcribed, will not get the changes.
  • Nodejs, storage is off by default, also no window object.
  • The code tries to be LOOKUP O(1) and broadcast O(N). Note, in order to get O(N) all callbacks get assigned to a setTimout to avoid stack fragmentation so plan accordingly and do not overload with complex operations on each callback.
  • Due to queuing up each callback after the broadcast happened, if your code changes the state value too fast, you might have a delayed effect on updates (You should get all messages in order)

Class methods

class ReHoard {

    constructor() {
        this._reHoardPubSub = new ReHoardPubSub();
    }

    /* Allow to change default settings. 
        settings = {
            persist : true,
            session : true,
            timeAlive : 1,
            undoRedo:  true,
            actionsHistory: true,
            actionsHistoryLimit:  100,
            typeMutable: false,
            production:  true
       };
   */
    changeSettings(settings) {
        this._reHoardPubSub.changeSettings(settings);
    }

    // Allows to create the state in a more explict way, a value must be passed to determine its type.
    // Dispatch will handle creation, so this is not needed.
    create(stateName, stateValue) {
        return this._reHoardPubSub._create(stateName, stateValue, "CREATION");
    }


    // dispatch changes, this will broadcast to any subscribers.
    // If state does not exists, it will create it, otherwise, it will update its value. 
    dispatch(stateName, stateValue, actionReference = "") {
        return this._reHoardPubSub.dispatch(stateName, stateValue, actionReference);
    }

    // subcribes to an existing state, if it does not exists it will throw an exception. 
    subscribe(stateName, listener, unSubscribeCB) {
        return this._reHoardPubSub.subscribe(stateName, listener, unSubscribeCB);
    }

    // will subscribe if the state exists, otherwise will queue it up once it is created. 
    subscribeWhenBecomesAlive(stateName, listener, unSubscribeCB) {
        return this._reHoardPubSub.subscribeWhenBecomesAlive(stateName, listener, unSubscribeCB);
    }

    // force a broadcast of the current value to everyone
    broadcastState(stateName) {
        return this._reHoardPubSub.broadcastState(stateName);
    }

    // returns current value of the state sync. No broadcasting happens
    getCurrentState(stateName) {
        return this._reHoardPubSub.getCurrentValue(stateName);
    }

    // Delete the current state and its listeners.
    deleteState(stateName) {
        return this._reHoardPubSub.deleteState(stateName);
    }

    // Redo the value state + action 
    redo(stateName) {
        return this._reHoardPubSub.redo(stateName);
    }

    // Undo the value to previous one
    undo(stateName) {
        return this._reHoardPubSub.undo(stateName);
    }

    // Prints all states
    getStatesNames(){
        return this._reHoardPubSub.getStatesNames();
    }
}

Installation

npm install rehoard --save

or

 <script src="dist/js/rehoard.min.js"></script>

Usage

ES6

import Rehoard from 'Rehoard';

const rehoard = new Rehoard();

WWW -- HEAD SCRIPT FIRST

var rehoard = new window.Rehoard();

Code Samples

Example 1

rehoard.create("testOne", 1);

const listener1 = rehoard.subscribe("testOne", function(value) {
    console.log(value);
});

rehoard.dispatch("testOne", 100); //console -> 100
rehoard.dispatch("testOne", 1000); //console -> 1000
rehoard.dispatch("testOne", 10000); //console -> 10000

listener1.unSubscribe();

rehoard.dispatch("testOne", 10); // nothing happens

Example 2 -- SUBCRIBE TO A FUTURE STATE

You may subscribe to a state which do not exists at that moment (data coming from other api's). This allows you to setup everything and respond once it exists accordingly

rehoard.subscribeWhenBecomesAlive("testOne", function(value) {
    console.log(value);
});

rehoard.create("testOne", 1);

rehoard.dispatch("testOne", 100); //console -> 100
rehoard.dispatch("testOne", 1000); //console -> 1000
rehoard.dispatch("testOne", 10000); //console -> 10000

Example 3 -- DISPATCH

Dispatch will create or update the state implicitly, so Reahord.create() is useful to mark where we create the initial state if needed

rehoard.subscribeWhenBecomesAlive("testOne", function(value) {
    console.log(value);
});

rehoard.dispatch("testOne", 100); //console -> 100
rehoard.dispatch("testOne", 1000); //console -> 1000
rehoard.dispatch("testOne", 10000); //console -> 10000

Example 4 -- REDO/UNDO

rehoard.subscribeWhenBecomesAlive("testOne", function(value) {
    console.log(value);
});

rehoard.dispatch("testOne", 100); //console -> 100
rehoard.dispatch("testOne", 1000); //console -> 1000
rehoard.dispatch("testOne", 10000); //console -> 10000

rehoard.undo("testOne"); //console --> 1000
rehoard.undo("testOne"); //console --> 100
rehoard.undo("TestOne"); // Nothing
rehoard.redo("TestOne"); // console --> 100

Example 5 -- INMUTABILITY

By default, types cannot be mutaded. If behaviour is needed, you may change them in the settings

rehoard.subscribeWhenBecomesAlive("testOne", function(value) {
    console.log(value);
});

rehoard.dispatch("testOne", 100); //console -> 100
rehoard.dispatch("testOne", "hola"); // console --> Error if type inmutability is ON, otherwise "hola" 

Example 5 -- ACTION MESSAGES

You may pass Action messages to keep a history of what changed the data

rehoard.subscribeWhenBecomesAlive("testOne", function(value) {
    console.log(value);
});

rehoard.dispatch("testOne", 100, "Initial"); //console -> 100
rehoard.dispatch("testOne", 1000, "New value"); //console -> 1000
rehoard.dispatch("testOne", 10000, "Last value"); //console -> 10000

Note 1: I hate the name

Note 2: Not responsible for any damage or bug, it was never intended to be used by other people

Note 3: Tests are busted at the moment, no async check. Will fix them later.