Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deebloo/state-container
https://github.com/deebloo/state-container
Last synced: 28 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/deebloo/state-container
- Owner: deebloo
- Created: 2019-01-24T00:03:38.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T20:21:20.000Z (about 1 year ago)
- Last Synced: 2024-11-18T11:25:20.217Z (about 2 months ago)
- Language: TypeScript
- Size: 1.34 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# StateContainer [![CircleCI](https://circleci.com/gh/deebloo/state-container.svg?style=svg)](https://circleci.com/gh/deebloo/state-container)
### A state container is created by defining an initial state and a reducer
```TS
import { StateContainer, Action } from '@deebloo/state-container';const enum CounterTodoType { Increment, Decrement }
class Increment implements Action {
readonly type = CounterTodoType.Increment;
}class Decrement implements Action {
readonly type = CounterTodoType.Decrement;
}const container = new StateContainer(0, (state, action) => {
switch(action.type) {
case CounterTodoType.Increment:
return state + 1;case CounterTodoType.Increment:
return state + 1;
}return state;
});
```### Get state by subscribing to the value
```TS
// A raw action
container.value.subscribe(console.log);
```### Update State by passing it a StateResult which is either:
```TS
// A raw action
container.update(new Increment());
``````TS
// A function that returns an action
container.update(() => new Increment());
``````TS
// A function that returns a promise that resolves to an action
container.update(() => fetch('/my-api').then(() => new Increment()));
``````TS
// A function that returns an Observable that resolves to an action
container.update(() => of('Hello').pipe(map(() => new Increment())));
```