store-and-pubsub

Library to store values in user defined paths, also can be subscribed to store published changes through pub sub flow

Usage no npm install needed!

<script type="module">
  import storeAndPubsub from 'https://cdn.skypack.dev/store-and-pubsub';
</script>

README

Store and PubSub

What is this?

A simple library to manage a Store and subscriptions to store values.

How to use it?

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

JSDOC

Table of Contents

pullFrom

Retrieves value from stored path.

Type: Function

Parameters
  • path string locator string path to store

Returns any can be anything stored at given path, anythng stored returns undefined

pushTo

Push value to path and notify if could publish.

Type: Function

Parameters
  • path string locator string path to store
  • newValue any the value to push
  • forceUpdate (optional, default false)

pushValuesTo

Push value to path.

Type: Function

Parameters
  • path string locator string path to store
  • values object contains multiple keys and values to be pushed to store

unsetPath

Removes path stored.

Type: Function

Parameters
  • path string locator string path to store

Returns boolean true if unset has been effective, otherwise returns false

subscribeToPath

Subscribes to stored path changes with given callback.

Type: Function

Parameters
  • path string locator string path to store
  • callback

Returns any reference to be used to single unsubscribe

unsubscribeFromPath

Unsubscribe from given paths were has subscribed.

Type: Function

Parameters
  • path string locator string path to store

removeSubscription

Unsubscribe from all paths were callback or subscriber has subscribed.

Type: Function

Parameters