All Projects → spierala → Mini Rx Store

spierala / Mini Rx Store

Licence: other
Lightweight Redux Store based on RxJS

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Mini Rx Store

ngx-mobx
Mobx decorators for Angular Applications
Stars: ✭ 14 (-56.25%)
Mutual labels:  rxjs, state-management
juliette
Reactive State Management Powered by RxJS
Stars: ✭ 84 (+162.5%)
Mutual labels:  rxjs, state-management
space-state
Demo app for Subjecting State to Good Behavior
Stars: ✭ 46 (+43.75%)
Mutual labels:  rxjs, state-management
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (+62.5%)
Mutual labels:  rxjs, state-management
Akita
🚀 State Management Tailored-Made for JS Applications
Stars: ✭ 3,338 (+10331.25%)
Mutual labels:  rxjs, state-management
ng-effects
Reactivity system for Angular. https://ngfx.io
Stars: ✭ 46 (+43.75%)
Mutual labels:  rxjs, state-management
aurelia-hoc-store
An Aurelia application showing the use of higher order components and a single state approach.
Stars: ✭ 20 (-37.5%)
Mutual labels:  rxjs, state-management
Bloc.js
A predictable state management library that helps implement the BLoC design pattern in JavaScript
Stars: ✭ 111 (+246.88%)
Mutual labels:  rxjs, state-management
reactive-store
A store implementation for state management with RxJS
Stars: ✭ 29 (-9.37%)
Mutual labels:  rxjs, state-management
ngx-model-hacker-news-example
Example repository with Hacker News implementation using Angular, Angular Material & ngx-model
Stars: ✭ 27 (-15.62%)
Mutual labels:  rxjs, state-management
Model
Angular Model - Simple state management with minimalist API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.
Stars: ✭ 242 (+656.25%)
Mutual labels:  rxjs, state-management
Platform
Reactive libraries for Angular
Stars: ✭ 7,020 (+21837.5%)
Mutual labels:  rxjs, state-management
Reactive State
Redux-clone build with strict typing and RxJS down to its core. Wrist-friendly, no boilerplate or endless switch statements
Stars: ✭ 135 (+321.88%)
Mutual labels:  rxjs, state-management
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (-40.62%)
Mutual labels:  rxjs, state-management
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (+303.13%)
Mutual labels:  rxjs, state-management
flow-state
UI state management with RxJS.
Stars: ✭ 33 (+3.13%)
Mutual labels:  rxjs, state-management
Sigi
Well designed effect management framework for complex frontend app
Stars: ✭ 95 (+196.88%)
Mutual labels:  rxjs, state-management
Store
Aurelia single state store based on RxJS
Stars: ✭ 103 (+221.88%)
Mutual labels:  rxjs, state-management
rxdeep
rxjs deep state management
Stars: ✭ 47 (+46.88%)
Mutual labels:  rxjs, state-management
Fluorine
[UNMAINTAINED] Reactive state and side effect management for React using a single stream of actions
Stars: ✭ 287 (+796.88%)
Mutual labels:  rxjs, state-management

MiniRx - RxJS Redux Store - Logo

NPM Downloads MIT License Tests All Contributors styled with prettier

MiniRx Store 2 (beta)

FOR 1.X PLEASE GO TO THE 1.x BRANCH

MiniRx Store provides Reactive State Management for Javascript Applications inspired by Redux. It is a global, application-wide solution to manage state and is powered by RxJS.

What's Included

  • RxJS powered global state management
  • State and Actions are exposed as RxJS Observable
  • Store (Redux API):
    • Actions
    • Reducers
    • Meta Reducers
    • Memoized Selectors
    • Effects
    • Support for ts-action: Create and consume actions with as little boilerplate as possible
  • FeatureStore: Update state without actions and reducers:
    • setState() update the feature state
    • select() read feature state
    • effect() run side effects like API calls and update feature state
    • undo() easily undo setState actions
  • Extensions:
    • Redux Dev Tools Extension: Inspect State with the Redux Dev Tools
    • Immutable Extension: Enforce immutability
    • Undo Extension: Undo dispatched Actions
    • Logger Extension: console.log the current action and updated state
  • Framework agnostic: MiniRx works with any front-end project built with JavaScript or TypeScript (Angular, Svelte, React, Vue, or anything else)
  • TypeScript support: The MiniRx API comes with TypeScript type definitions
  • Angular Integration: Use MiniRx Store the Angular way: StoreModule.forRoot(), StoreModule.forFeature(), ...

Key Concepts

  • The store is a single object which holds the global application state. It is the "single source of truth"
  • State is exposed as RxJS Observable
  • State has a flat hierarchy and is divided into "feature states" (also called "slices" in Redux world)
  • For each "feature state" we can decide to use the Redux API with actions and a reducer or the Feature Store API with setState
  • State is read-only (immutable) and can only be changed by dispatching actions (Redux API) or by using setState (Feature Store API)

Installation

Install from the NPM repository using npm:

npm install [email protected]

Install the RxJS peer dependency:

npm install rxjs

Basic Tutorial

Let's dive into some code to see MiniRx in action

Store (Redux API)

MiniRx supports the classic Redux API with registering reducers and dispatching Actions. Observable state can be selected with memoized selectors.

import { Action, Store, configureStore, createFeatureSelector, createSelector } from "mini-rx-store";
import { Observable } from "rxjs";

// 1.) State interface
interface CounterState {
  count: number;
}

// 2.) Initial state
const counterInitialState: CounterState = {
  count: 1
};

// 3.) Reducer
function counterReducer(
  state: CounterState = counterInitialState,
  action: Action
): CounterState {
  switch (action.type) {
    case "inc":
      return {
        ...state,
        count: state.count + 1
      };
    default:
      return state;
  }
}

// 4.) Get hold of the store instance and register root reducers
const store: Store = configureStore({
  reducers: {
    counter: counterReducer
  }
});

// 5.) Create memoized selectors
const getCounterFeatureState = createFeatureSelector<CounterState>("counter");
const getCount = createSelector(
  getCounterFeatureState,
  state => state.count
);

// 6.) Select state as RxJS Observable
const count$: Observable<number> = store.select(getCount);
count$.subscribe(count => console.log('count:', count));

// 7.) Dispatch an action
store.dispatch({ type: "inc" });

// OUTPUT: count: 1
// OUTPUT: count: 2

Feature Store API

FeatureStore allows us to manage feature state without actions and reducers. The API of a FeatureStore is optimized to select and update a feature state directly with a minimum of boilerplate.

import { FeatureStore } from "mini-rx-store";
import { Observable } from "rxjs";

// 1.) State interface
interface CounterState {
  counter: number;
}

// 2.) Initial state
const counterInitialState: CounterState = {
  counter: 11
};

export class CounterFeatureStore extends FeatureStore<CounterState> {

  // Select state as RxJS Observable
  counter$: Observable<number> = this.select(state => state.counter);

  constructor() {
    super('counterFs', counterInitialState)
  }

  // Update state with `setState`
  inc() {
    this.setState(state => ({...state, counter: state.counter + 1}))
  }
}

Use the "counterFs" feature store like this:

import { CounterFeatureStore } from "./counter-feature-store";

const counterFs = new CounterFeatureStore();
counterFs.counter$.subscribe(count => console.log('count:', count));
counterFs.inc();

// OUTPUT: count: 11
// OUTPUT: count: 12

ℹ The FeatureStore states become part of the global state too.

Both the Redux feature state and the FeatureStore state are living next to each other in the global state object:

store.select(state => state).subscribe(console.log);

//OUTPUT: {"counter":{"count":2},"counterFs":{"counter":12}}

MiniRx Blog Posts:

References

These projects, articles and courses helped and inspired us to create MiniRx:

License

MIT

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Pieter Van Poyer

💻

Florian Spier

💻 🤔

Carsten

🎨

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].