Almin's reduce store library.
It is similar with Facebook flux's ReduceStore.
Install with npm:
npm install almin-reduce-store
There are two components - Store
and State
.
ExampleState
:
"use strict";
import {ReduceState} from "almin-reduce-store";
import IncrementUseCase from "./IncrementUseCase";
import DecrementUseCase from "./DecrementUseCase";
export default class ExampleState extends ReduceState {
constructor({
count = 0 // initial state
} = {}) {
super();
this.count = count;
}
reduce(payload) {
// when can handle payload, should return new state
switch (payload.type) {
case IncrementUseCase.Events.increment:
return new ExampleState(Object.assign({}, this, {
count: this.count + 1
}));
case DecrementUseCase.Events.decrement:
return new ExampleState(Object.assign({}, this, {
count: this.count - 1
}));
default: // when other payload, should return same state
return this;
}
}
}
ReduceStore
:
// LICENSE : MIT
"use strict";
import {ReduceStore} from "almin-reduce-store";
import ExampleState from "./ExampleState";
export default class ExampleStore extends ReduceStore {
constructor() {
super();
this.state = new ExampleState();
}
getState() {
return {
example: this.state
};
}
}
When execute IncrementUseCase and ExampleStore#getState
return new State of ExampleState
.
See Example for details.
See Releases page.
Install devDependencies and Run npm test
:
npm i -d && npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © azu