Giter VIP home page Giter VIP logo

datastore's Introduction

datastore

CI

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

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 state in an application 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 ๐Ÿš€

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

license

MIT

datastore's People

Contributors

svaza avatar dependabot[bot] avatar

Stargazers

 avatar Kipruto avatar

Watchers

 avatar

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.