Coder Social home page Coder Social logo

yamalight-rxstate's Introduction

RxState

npm MIT Build Status Coverage Status

Simple opinionated state management library based on RxJS

Installation

npm install --save rxstate rxjs

Quick start

Example code for creating a store with status and typeahead fetching action is shown below:

import fetchival from 'fetchival';
import {from} from 'rxjs';
import {map, filter, debounceTime, distinctUntilChanged, tap, flatMap} from 'rxjs/operators';
import {createStore, createAction, createStatus} from 'rxstate';

// create status action
const status = createStatus();

// create action that fetches typeahead suggestions from server
const getTypeahead = createAction();
const typeahead$ = getTypeahead.$.pipe(
  map(e => e.target.value),
  filter(q => q.length > 3),
  debounceTime(300),
  distinctUntilChanged(),
  tap(() => status('loading')),
  flatMap(q => from(fetchival(typeaheadAPI).post({q}))),
  tap(() => status('done'))
);

// create an array of action streams for store
const streams = [status.$, typeahead$];
// create store
const store = createStore({streams, defaultState: {init: true}});

// other place in code:
// subscribe for state updates
store.subscribe(state => {
  console.log(state);
  // ... handle your state here
});

// other place in code:
// trigger action
getTypeahead('keyword');

Things to keep in mind

  • Rxstate has RxJS as peer dependency - don't forget to install it as well!
  • Store will always return last value to new subscribers.
  • By default, the state is updated using spread operator on new and old state (e.g. {...oldState, ...newState}). You can change that by passing combinator parameter during store creation, e.g.:
// create combinator that always returns new state
const combinator = (_, data) => data;
// create store
const store = createStore({streams, defaultState, combinator});
  • Status action and stream can be created using createStatus function. By default it'll write status as {status: 'statusText'}. Key can be changed by passing the string parameter to the function, e.g.:
// create status with custom key
const status = createStatus('customStatus');
// state will be updated with {customStatus: 'statusText'}
  • Stores have .clear() method that accepts new initial state as an optional argument and dispatches new action with either provided or default state as value. If you use default combinator logic - this will reset your state to initial one.

License

MIT

yamalight-rxstate's People

Contributors

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