{"id":23338202,"url":"https://github.com/tacho87/simplypubsubjs","last_synced_at":"2026-05-05T14:32:24.584Z","repository":{"id":57352560,"uuid":"66440783","full_name":"tacho87/SimplyPubSubJs","owner":"tacho87","description":"ReHoard is a simply pub/sub lib for js and node","archived":false,"fork":false,"pushed_at":"2019-03-09T18:39:49.000Z","size":305,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-29T15:03:09.938Z","etag":null,"topics":["flux","javascript","nodejs","observable","publish-subscribe","pubsub","react","reactjs","redux","vanilla-javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tacho87.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-24T07:18:45.000Z","updated_at":"2023-04-11T15:19:11.000Z","dependencies_parsed_at":"2022-09-16T08:10:36.729Z","dependency_job_id":null,"html_url":"https://github.com/tacho87/SimplyPubSubJs","commit_stats":null,"previous_names":["tacho87/rehoard"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tacho87/SimplyPubSubJs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tacho87%2FSimplyPubSubJs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tacho87%2FSimplyPubSubJs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tacho87%2FSimplyPubSubJs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tacho87%2FSimplyPubSubJs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tacho87","download_url":"https://codeload.github.com/tacho87/SimplyPubSubJs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tacho87%2FSimplyPubSubJs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261853230,"owners_count":23219826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flux","javascript","nodejs","observable","publish-subscribe","pubsub","react","reactjs","redux","vanilla-javascript"],"created_at":"2024-12-21T03:12:44.395Z","updated_at":"2026-04-14T04:31:03.253Z","avatar_url":"https://github.com/tacho87.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimplyPubSubJs (REHOARD)\n\n## Introduction\n\n**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).\n\nIt is a **simply pub/sub** for **ReactJs**,  **Vanilla JS** and **NodeJs**. It support local/session storage with redo/undo of actions. \n\n### *Some features*\n* **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**)\n* 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. \n* **Nodejs**, storage is **off** by default, also no window object.\n* 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. \n* 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)\n\n---\n## Class methods\n\n\n\n```javascript\nclass ReHoard {\n\n    constructor() {\n        this._reHoardPubSub = new ReHoardPubSub();\n    }\n\n    /* Allow to change default settings. \n        settings = {\n            persist : true,\n            session : true,\n            timeAlive : 1,\n            undoRedo:  true,\n            actionsHistory: true,\n            actionsHistoryLimit:  100,\n            typeMutable: false,\n            production:  true\n       };\n   */\n    changeSettings(settings) {\n        this._reHoardPubSub.changeSettings(settings);\n    }\n\n    // Allows to create the state in a more explict way, a value must be passed to determine its type.\n    // Dispatch will handle creation, so this is not needed.\n    create(stateName, stateValue) {\n        return this._reHoardPubSub._create(stateName, stateValue, \"CREATION\");\n    }\n\n\n    // dispatch changes, this will broadcast to any subscribers.\n    // If state does not exists, it will create it, otherwise, it will update its value. \n    dispatch(stateName, stateValue, actionReference = \"\") {\n        return this._reHoardPubSub.dispatch(stateName, stateValue, actionReference);\n    }\n\n    // subcribes to an existing state, if it does not exists it will throw an exception. \n    subscribe(stateName, listener, unSubscribeCB) {\n        return this._reHoardPubSub.subscribe(stateName, listener, unSubscribeCB);\n    }\n\n    // will subscribe if the state exists, otherwise will queue it up once it is created. \n    subscribeWhenBecomesAlive(stateName, listener, unSubscribeCB) {\n        return this._reHoardPubSub.subscribeWhenBecomesAlive(stateName, listener, unSubscribeCB);\n    }\n\n    // force a broadcast of the current value to everyone\n    broadcastState(stateName) {\n        return this._reHoardPubSub.broadcastState(stateName);\n    }\n\n    // returns current value of the state sync. No broadcasting happens\n    getCurrentState(stateName) {\n        return this._reHoardPubSub.getCurrentValue(stateName);\n    }\n\n    // Delete the current state and its listeners.\n    deleteState(stateName) {\n        return this._reHoardPubSub.deleteState(stateName);\n    }\n\n    // Redo the value state + action \n    redo(stateName) {\n        return this._reHoardPubSub.redo(stateName);\n    }\n\n    // Undo the value to previous one\n    undo(stateName) {\n        return this._reHoardPubSub.undo(stateName);\n    }\n\n    // Prints all states\n    getStatesNames(){\n        return this._reHoardPubSub.getStatesNames();\n    }\n}\n```\n\n\n---\n\n## Installation\n```javascript\nnpm install rehoard --save\n```\nor \n```javascript\n \u003cscript src=\"dist/js/rehoard.min.js\"\u003e\u003c/script\u003e\n```\n---\n## Usage\n\n### ES6\n```javascript\nimport Rehoard from 'Rehoard';\n\nconst rehoard = new Rehoard();\n```\n\n\n### WWW -- HEAD SCRIPT FIRST\n```javascript\nvar rehoard = new window.Rehoard();\n```\n\n---\n## Code Samples\n\n## Example 1\n```javascript\nrehoard.create(\"testOne\", 1);\n\nconst listener1 = rehoard.subscribe(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.dispatch(\"testOne\", 100); //console -\u003e 100\nrehoard.dispatch(\"testOne\", 1000); //console -\u003e 1000\nrehoard.dispatch(\"testOne\", 10000); //console -\u003e 10000\n\nlistener1.unSubscribe();\n\nrehoard.dispatch(\"testOne\", 10); // nothing happens\n```\n\n## Example 2 -- SUBCRIBE TO A FUTURE STATE\n*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*\n```javascript\nrehoard.subscribeWhenBecomesAlive(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.create(\"testOne\", 1);\n\nrehoard.dispatch(\"testOne\", 100); //console -\u003e 100\nrehoard.dispatch(\"testOne\", 1000); //console -\u003e 1000\nrehoard.dispatch(\"testOne\", 10000); //console -\u003e 10000\n```\n\n\n## Example 3 -- DISPATCH\n*Dispatch will create or update the state implicitly, so **Reahord.create()** is useful to mark where we create the initial state if needed* \n```javascript\nrehoard.subscribeWhenBecomesAlive(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.dispatch(\"testOne\", 100); //console -\u003e 100\nrehoard.dispatch(\"testOne\", 1000); //console -\u003e 1000\nrehoard.dispatch(\"testOne\", 10000); //console -\u003e 10000\n\n```\n\n## Example 4 -- REDO/UNDO\n\n```javascript\nrehoard.subscribeWhenBecomesAlive(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.dispatch(\"testOne\", 100); //console -\u003e 100\nrehoard.dispatch(\"testOne\", 1000); //console -\u003e 1000\nrehoard.dispatch(\"testOne\", 10000); //console -\u003e 10000\n\nrehoard.undo(\"testOne\"); //console --\u003e 1000\nrehoard.undo(\"testOne\"); //console --\u003e 100\nrehoard.undo(\"TestOne\"); // Nothing\nrehoard.redo(\"TestOne\"); // console --\u003e 100\n```\n\n## Example 5 -- INMUTABILITY\n*By default, types cannot be mutaded. If behaviour is needed, you may change them in the settings*\n```javascript\nrehoard.subscribeWhenBecomesAlive(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.dispatch(\"testOne\", 100); //console -\u003e 100\nrehoard.dispatch(\"testOne\", \"hola\"); // console --\u003e Error if type inmutability is ON, otherwise \"hola\" \n```\n\n## Example 5 -- ACTION MESSAGES\n*You may pass Action messages to keep a history of what changed the data*\n```javascript\nrehoard.subscribeWhenBecomesAlive(\"testOne\", function(value) {\n    console.log(value);\n});\n\nrehoard.dispatch(\"testOne\", 100, \"Initial\"); //console -\u003e 100\nrehoard.dispatch(\"testOne\", 1000, \"New value\"); //console -\u003e 1000\nrehoard.dispatch(\"testOne\", 10000, \"Last value\"); //console -\u003e 10000\n```\n\n\n---\n***Note 1:** I hate the name*\n\n***Note 2:** Not responsible for any damage or bug, it was never intended to be used by other people*\n\n***Note 3:** Tests are busted at the moment, no async check. Will fix them later.*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftacho87%2Fsimplypubsubjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftacho87%2Fsimplypubsubjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftacho87%2Fsimplypubsubjs/lists"}