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

@svaza/datastore

v1.1.1

Published

A simple javascript library for managing the state. Nothing is out of the box, all is the play of rxjs BehaviorSubject<>

Downloads

17

Readme

datastore

CI

A simple javascript library for managing the state. Nothing is out of the box, all is the play of rxjs BehaviorSubject<> :tada:

design and inspiration

To be point straight IDataStore implementation was developed to keep state management more simple, hence it works on principle of mutating the data, mutation wont work with simple primitive types because the way they are managed in javascript. Its developer's responsibility to notify all observers of a key if in code the complex object is changed in any way.

Managing application state can sometime become a complex problem, especially when there is a requirement to perform an action when data changes overtime. An action can be as simple as updating a UI component when it happens.

Sometimes to support the principle of not mutating an object one might end up structuring an app in a particular way which might make code more complex, For example, A UI component might be unnecessarily broken into tens of components to support this principle.

Rxjs actually makes the statemanagement more simple and this library is just a small wrapper which manages state centrally, and provides few helpful methods to work with the state

This can be easily achieved by directly using rxjs, however one of the important design requirement is to have a data structure which helps managing data and subscriptions centrally which IDataStore helps in doing that.

how it works

Uses rxjs for managing subscriptions centrally. Every data is identified by a key (of string type), same key can be used to register subscriptions over the data, or perform any of the operations as defined by IDataStore data structure.

installation

    npm i @svaza/datastore

dependencies

important!!

Please go through complete usage below to understand how this library works

usage

  • For implementation in angular, refer examples/angular
  • Simply create an instance of required type of store implementation
    let store: IDataStore = new MemoryStore();
  • Register subscription over a key, the subscription will be immediately called as underlying channel is BehaviourSubject<>, to ignore initial subscription incase when there is no data, perform a check on the data provided, if its undefined then simply ignore it. If the required key already has data associated with it then subscription will be called immediately with respective data - As underlying channel is BehaviourSubject<>
    var subscription = store.observe('<key>')
                            .subscribe(data => {
                                // handle the data change
                                // ignore if data is undefined
                                if(data === undefined) return;
                            });
    // stop observing, if needed
    subscription.unsubscibe();
  • Add some data
    // adding data notifies all observers of that key
    store.add<SomeType>('<key>', { message: 'hello world' });

    // add data without notifying all observers of that key
    store.addSilently<SomeType>('<key>', { message: 'hello world' });
  • Simply get the data for the given key.

Important thing to note here is that get() returns the actual underlying reference to the data, So if the respective data is a complex object then changing any of its properties will also change underlying data, i.e javascript reference types and primitive types rules applies here too. In this situation you might need to manually notify all obsrvers of the key as explained in Mutating the data and notifying observers usecase below

    let data: SomeModel = store.get<SomeModel>('<key>');
  • Mutating the data and notifying observers
    let data: SomeModel = store.get<SomeModel>('<key>');
    data.message = 'hello universe'; // at this point none of the observers will be notified
    // its dev responsibility to manually notify all observers using notify()
    store.notify('<some key>');
  • Removing a key
    // removing a key also unsubscribes the underlying Subject associated with that key, Once key is removed, all underlying observers becomes stale and adding data under same key again will have no effect
    // useful during dispose
    store.remove('key');
  • Get list of keys
    string[] keys = store.keys();

maintainers

version history

  • v1.1.0

    • code comments
    • make add() and addSilently() generic
    • example angular project
  • v1.0.0 :rocket:

    • initial version
    • support for memory store
    • basic store data structure

license

MIT