almin-reduce-store
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

almin-reduce-store Build Status

Almin's reduce store library.

It is similar with Facebook flux's ReduceStore.

Install

Install with npm:

npm install almin-reduce-store

Usage

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.

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm i -d && npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Readme

Keywords

Package Sidebar

Install

npm i almin-reduce-store

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • azu