A simple library to manage a Store and subscriptions to store values.
First you need to import it in your project
The require way
let { pullFrom, pushTo } = require("store-and-pubsub");The import way
import { unsetPath } from "store-and-pubsub";Then use it to establish a store with getter and setter for a path, for example currentParam
import { pullFrom, pushTo } from "store-and-pubsub";
const MODULE_STORE_NAME = 'moduleStoreName'
// INITIALIZE AN EMPTY STORE FOR YOUR MODULE
pushTo(MODULE_STORE_NAME, {})
// CREATE A SETTER FOR SOME PATH TO STORE
pushTo(`${MODULE_STORE_NAME}.currentParam`, state)
// CREATE A GETTER FOR SOME PATH TO RETRIEVE
pullFrom(`${MODULE_STORE_NAME}.currentParam`)To unset or push multiple values to store
import { unsetPath, pushValuesTo } from "store-and-pubsub";
const MODULE_STORE_NAME = 'moduleStoreName'
// REMOVES THE PROPERTY AT PATH OF STORE
unsetPath(`${MODULE_STORE_NAME}.currentParam`)
// PUSHES MULTIPLE PROPERTIES AND VALUES AT PATH OF STORE
const values = {
prop1: "value1",
prop2: "value2",
}
pushValuesTo(`${MODULE_STORE_NAME}`, values)Paths from store can be subscribed for changes
import { subscribeToPath, unsubscribeFromPath, removeSubscription } from "store-and-pubsub";
const MODULE_STORE_NAME = 'moduleStoreName'
const path = `${MODULE_STORE_NAME}.currentParam`
const myCallback = () => {
// YOUR OWN CODE AND STUFF
}
// SUBSCRIBES TO moduleStoreName.currentParam CHANGES EXECUTING myCallback
const subscriber = subscribeToPath(path, myCallback)
// REMOVES SUBSCRIPTION FRM GIVEN PATH
unsubscribeFromPath(path)
// GIVEN subscriber REMOVES IT FROM PATH SUBSCRIPTION
removeSubscription(subscription)
// GIVEN myCallback REMOVES IT FROM ALL PATHS SUBSCRIBED
removeSubscription(myCallback)Powered by xiscodev
Additional JSDOC info
Retrieves value from stored path.
Type: Function
pathstring locator string path to store
Returns any can be anything stored at given path, anythng stored returns undefined
Push value to path and notify if could publish.
Type: Function
pathstring locator string path to storenewValueany the value to pushforceUpdate(optional, defaultfalse)
Push value to path.
Type: Function
pathstring locator string path to storevaluesobject contains multiple keys and values to be pushed to store
Removes path stored.
Type: Function
pathstring locator string path to store
Returns boolean true if unset has been effective, otherwise returns false
Subscribes to stored path changes with given callback.
Type: Function
pathstring locator string path to storecallback
Returns any reference to be used to single unsubscribe
Unsubscribe from given paths were has subscribed.
Type: Function
pathstring locator string path to store
Unsubscribe from all paths were callback or subscriber has subscribed.
Type: Function