Git Product home page Git Product logo

store-and-pubsub's Introduction

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

store-and-pubsub's People

Contributors

xoxefdp avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

xoxefdp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.