npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yetanotherstore

v1.3.0

Published

A key value store in non persistant memory, with events, gatekeeper and lock.

Downloads

4

Readme

Yetanotherstore is a simple non persistent way to store global states and to attach events on what is happening to this storage. It was originally designed to be used in React to avoid the complexity of other lib (i'm looking at you Redux!), though it can easily be used without React.

npm install --save yetanotherstore

Concept

Yetanotherstore provides a unique singleton-like dictionary (key-values). There are some built in methods to add, remove, check and list properties. This guaranties that the key-values are never edited without the help of the provided methods.

In addition, some events are going to be emitted every time the storage is modified. See the event section.

Create a Store

First, you need to create a Store instance in a place where it's going to be accessible by the components that need it. Let's create a file AppStore.js that will instantiate Store and export the store object:

// AppStore.js

import Store from 'yetanotherstore'

let AppStore = new Store()
export default AppStore

Now, every file that import AppStore.js will be able to use the Store instance and access the same pool of data.

Methods

import AppStore from './AppStore'

// ...

// Add something to the store
AppStore.set('someKey', 'some value')
AppStore.set('anotherKey', {firstname: 'Johnny', lastname: 'Bravo'})

// get a value
les someVal = AppStore.get('someKey')

// Remove something from the store
AppStore.delete('someKey')

// check is the store has some key
if(AppStore.has('someKey')){
  // ...
}

// Get an array with all the keys
let allTheKeys = AppStore.keys()

// Reset the store so that it becomes empty
AppStore.reset()

Events

All the following event can accept multiple callback, there is no limit. Read more about the event engine here.

valueSet

Everytime a value is added or set in the store with the method Store.set(), the event valueSet is emitted. To subscribe to this event, do the following:

import AppStore from './AppStore'
AppStore.onValueSet((data) => {
  // ...
})

Then, the object data contains the properties:

  • key: the key that was just added or modified
  • value: the corresponding value
  • previousValue: the previous value, if there was already something corresponding to this key (undefined otherwise)

valueDeleted

When a value is removed from the Store with the method Store.delete(), the event valueDeleted is emitted. To subscribe to this event, do the following:

import AppStore from './AppStore'
AppStore.onValueDeleted((data) => {
  // ...
})

Then, the object data contains the properties:

  • key: the key that was just deleted
  • value: the corresponding value

reseted

When the whole store is reset with the method Store.reset(), the event reseted is emitted. To subscribe to this event, do the following:

import AppStore from './AppStore'
AppStore.onReseted(() => {
  // ...
})

This callback has no argument.

onSet for a specific key

To watch a particular key in the store, we can use the method .onSet(key, fn) as follow:

import AppStore from './AppStore'
AppStore.onSet('age', (data) => {
  // The available data are:
  // - data.key
  // - data.value
  // - data.previousValue
})

onDelete for a specific key

To watch when a particular key in the store is being deleted, we can use the method .onDelete(key, fn) as follow:

import AppStore from './AppStore'
AppStore.onDelete('age', (data) => {
  // The available data are:
  // - data.key
  // - data.value (the last value it had before being deleted)
})

Extra

Gatekeeper

A store provides the possibility to add a gatekeeper function, to make sure that some verifications is performed before stuff are being pushed into the store. The gatekeeper function takes two argument: key and 'value' and must return 'true' (the data is valid and can be pushed into the store) or 'false' (the data is invalid and cannot be pushed).

If the gatekeeper refuses to let a piece of data in, the event refused is emited. Let's see an example:

let store = new yetanotherstore()

// From now on, the gatekeeper accepts only values that are of type string
store.setGateKeeper((key, value) => {
  return typeof value === 'string'
})

// the callback when a value is refused by the gatekeeper function
store.on('refused', data => {
  console.log(`Nope, the value ${data.value} is not a string`)
})

// Later, for some reason, we want to remove the gatekeeper:
store.setGateKeeper(null)

Lock

This is to prevent the store to be set any values with the regular .set(key, value) method. But, if we use the forceLock version, we can still push things in:

let store = new yetanotherstore()

// lock the store, no one can push data inthere unless they use .set(key, value, true)
// with true being the 'forceLock' argument
store.lock()

// this won't add anything to the store
store.set('myKey', 32)

// but this will:
store.set('myForcedKey', 32, true)

// Then, other methods are available:
store.unlock()
let isItLocked = store.isLocked() // true or false

The React usecase

Yetanotherstore can be used across components as a global store, such that a value added by a component trigger an event into one or multiple other components.