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
{ this_reHoardPubSub = ; } /* Allow to change default settings. settings = { persist : true, session : true, timeAlive : 1, undoRedo: true, actionsHistory: true, actionsHistoryLimit: 100, typeMutable: false, production: true }; */ { this_reHoardPubSub; } // 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. { return this_reHoardPubSub; } // dispatch changes, this will broadcast to any subscribers. // If state does not exists, it will create it, otherwise, it will update its value. { return this_reHoardPubSub; } // subcribes to an existing state, if it does not exists it will throw an exception. { return this_reHoardPubSub; } // will subscribe if the state exists, otherwise will queue it up once it is created. { return this_reHoardPubSub; } // force a broadcast of the current value to everyone { return this_reHoardPubSub; } // returns current value of the state sync. No broadcasting happens { return this_reHoardPubSub; } // Delete the current state and its listeners. { return this_reHoardPubSub; } // Redo the value state + action { return this_reHoardPubSub; } // Undo the value to previous one { return this_reHoardPubSub; } // Prints all states { return this_reHoardPubSub; }
Installation
npm install rehoard --save
or
<script src="dist/js/rehoard.min.js"></script>
Usage
ES6
; const rehoard = ;
WWW -- HEAD SCRIPT FIRST
var rehoard = ;
Code Samples
Example 1
rehoard; const listener1 = rehoard; rehoard; //console -> 100rehoard; //console -> 1000rehoard; //console -> 10000 listener1; rehoard; // 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; rehoard; rehoard; //console -> 100rehoard; //console -> 1000rehoard; //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; rehoard; //console -> 100rehoard; //console -> 1000rehoard; //console -> 10000
Example 4 -- REDO/UNDO
rehoard; rehoard; //console -> 100rehoard; //console -> 1000rehoard; //console -> 10000 rehoard; //console --> 1000rehoard; //console --> 100rehoard; // Nothingrehoard; // console --> 100
Example 5 -- INMUTABILITY
By default, types cannot be mutaded. If behaviour is needed, you may change them in the settings
rehoard; rehoard; //console -> 100rehoard; // 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; rehoard; //console -> 100rehoard; //console -> 1000rehoard; //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.