Git Product home page Git Product logo

store's Introduction

npm GitHub browser node TypeScript

@axtk/store

A lightweight storage for shared state

An instance of the Store class is a lightweight container for data, which:

  • exposes a set of methods to manipulate the stored data (like .set(keyPath, data), .get(keyPath)), and
  • allows for subscriptions to updates it receives (via .onUpdate(handler)).

As an example, instances of the Store class can be used as a storage for shared state in React applications. See also react-store that adds a React hook for shared state management based on the Store class.

Store API

Initialization

import {Store} from '@axtk/store';

const store = new Store();
// With an initial state:
// const store = new Store({location: {x: -1, y: 1}});

The setters and getters below make use of the key paths to access the nested values. With TypeScript, the key paths, the arguments, and the return values are automatically checked to match the type of the store state.

Setters

store.setState({location: {x: 1, y: 0}});
store.set('location', {x: -1, y: 1});
store.set(['location', 'x'], 1);

Getters

let state = store.getState();
let location = store.get('location');
let y = store.get(['location', 'y']);
// With a default value:
let z = store.get(['location', 'z'], 0);

Subscription

let unsubscribe = store.onUpdate(store => {
    console.log('updated');
});
unsubscribe(); // to remove the subscription

Removal

store.removeState();
store.remove('location');
store.remove(['location', 'y']);

store's People

Contributors

axtk avatar

Watchers

 avatar  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.