Last active
November 19, 2020 03:36
-
-
Save exbotanical/c231bdd45f0f9329f634e19f3054ebb9 to your computer and use it in GitHub Desktop.
A super tiny events dispatcher and data store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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