MCPcopy Index your code
hub / github.com/Dynalon/reactive-state

github.com/Dynalon/reactive-state @v4.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.0.0 ↗ · + Follow
124 symbols 277 edges 26 files 11 documented · 9% updated 3y ago★ 1372 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Build Status npm version code coverage

Reactive State

A typed, wrist-friendly state container aimed as an alternative to Redux when using RxJS. Written with RxJS in TypeScript but perfectly usable from plain JavaScript. Originally inspired by the blog posting from Michael Zalecki but heavily modified and extended since.

Features

  • type-safe actions: no boilerplate code, no mandatory string constants, and not a single switch statement
  • Actions are just Observables, so are Subjects. Just call .next() to dispatch an action.
  • dynamically add and remove reducers during runtime
  • no need for async middlewares such as redux-thunk/redux-saga; actions are Observables and can be composed and transformed async using RxJS operators
  • no need for selector libraries like MobX or Reselect, RxJS already ships it
  • single, application-wide Store concept as in Redux. Possibility to create slices/substates for decoupling (easier reducer composition and state separation by module)
  • Strictly typed to find errors during compile time
  • Heavily unit tested, 100+ tests for ~250 lines of code
  • React bridge (like react-redux) included, though using React is not mandatory
  • Support for React-Devtool Extension

Installation

npm install reactive-state

Documentation

Additionally, there is a small example.ts file and see also see the included unit tests as well.

Example Usage

import { Store } from "reactive-state";
import { Subject } from "rxjs";
import { take } from "rxjs/operators";

// The state for our example app
interface AppState {
    counter: number;
}

const initialState: AppState = { counter: 0 }

const store = Store.create(initialState);

// The .watch() function returns an Observable that emits the selected state change, so we can subscribe to it
store.watch().subscribe(newState => console.log("STATE:", JSON.stringify(newState)));

// the watch() observable always caches the last emitted state, so we will immediately print our inital state:
// [CONSOLE.LOG]: STATE: {"counter":0}

// use a RxJS Subjects as an action
const incrementAction = new Subject<number>();

// A reducer is a function that takes a state and an optional payload, and returns a new state
function incrementReducer(state, payload) {
    return { ...state, counter: state.counter + payload };
};

store.addReducer(incrementAction, incrementReducer);

// lets dispatch some actions

incrementAction.next(1);
// [CONSOLE.LOG]: STATE: {"counter":1}
incrementAction.next(1);
// [CONSOLE.LOG]: STATE: {"counter":2}

// async actions? No problem, no need for a "middleware", just use RxJS
interval(1000).pipe(take(3)).subscribe(() => incrementAction.next(1));
// <PAUSE 1sec>
// [CONSOLE.LOG]: STATE: {"counter":3}
// <PAUSE 1sec>
// [CONSOLE.LOG]: STATE: {"counter":4}
// <PAUSE 1sec>
// [CONSOLE.LOG]: STATE: {"counter":5}

License

MIT.

Extension points exported contracts — how you extend this code

ConnectResult (Interface)
(no doc)
react/connect.tsx
StoreProviderProps (Interface)
(no doc)
react/provider.tsx
AppState (Interface)
(no doc)
test/test_example.ts
ExampleState (Interface)
(no doc)
test/test_common_types.ts
SliceState (Interface)
(no doc)
test/test_destroy.ts
GenericState (Interface)
(no doc)
test/test_action.ts
TestState (Interface)
(no doc)
test/test_react_connect.tsx
AppState (Interface)
(no doc)
src/example.ts

Core symbols most depended-on inside this repo

select
called by 63
src/store.ts
createSlice
called by 53
src/store.ts
addReducer
called by 52
src/store.ts
create
called by 43
src/store.ts
destroy
called by 29
src/store.ts
watch
called by 10
src/store.ts
connect
called by 9
react/connect.tsx
dispatch
called by 8
src/store.ts

Shape

Function 46
Method 35
Interface 23
Class 20

Languages

TypeScript100%

Modules by API surface

src/store.ts21 symbols
react/provider.tsx20 symbols
react/connect.tsx17 symbols
test/test_react_connect.tsx11 symbols
src/example.ts8 symbols
test/test_example.ts7 symbols
test/test_reducer.ts4 symbols
test/test_react_storeprovider.tsx4 symbols
test/test_initial_state.ts4 symbols
test/test_common_types.ts4 symbols
src/devtool.ts4 symbols
test/test_select.ts3 symbols

For agents

$ claude mcp add reactive-state \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact