Skip to content

Instantly share code, notes, and snippets.

@exbotanical
Last active November 19, 2020 03:36
Show Gist options
  • Select an option

  • Save exbotanical/c231bdd45f0f9329f634e19f3054ebb9 to your computer and use it in GitHub Desktop.

Select an option

Save exbotanical/c231bdd45f0f9329f634e19f3054ebb9 to your computer and use it in GitHub Desktop.
A super tiny events dispatcher and data store
const init = (db) => {
db.actions = {};
db.store = {};
db.register = (name, fn) => {
db.actions[name] = fn;
return db;
}
db.dispatch = (opt) => {
const action = db.actions[opt];
if (action) action();
return db;
};
db.unregister = (name) => {
delete db.actions[name];
return db;
};
db.set = (key, value) => {
db.store[key] = value;
};
db.get = (key) => db.store[key];
db.getStore = () => db.store;
return db;
};
module.exports = () => init({});
/*
const dispatcher = require("tiny-dispatcher");
const db = dispatcher();
db.set("a", { a: 1, b: 2 });
function testD () {
console.log(db.get("a"));
}
db.register("testD", testD).dispatch("testD").unregister("testD");
db.dispatch("testD");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment