Giter Club home page Giter Club logo

simplypubsubjs's Introduction

SimplyPubSubJs (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.

simplypubsubjs's People

Contributors

tacho87 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.