@nathanfaucett/state-immutable
    Overview
    Chat
    Issues
    Code
    Discussions
    Contributors
    Alternatives
    Dependencies
    Tutorials

@nathanfaucett/state-immutable

immutable state management for applications

0.1.3  •  Updated 2 years ago  •  by Nathan Faucett  •  MIT License

js-state-immutable

immutable state management for applications

States

a State is the single source of truth for application, states have stores which are used to update the state.

createStore(name: String[, initialState: Object | Immutable.Map]) -> Store
removeStore(name: String)

Example

import State from "@nathanfaucett/state-immutable";

const state = new State();
const store = state.createStore("storeName", {
    key: "value"
});

state.on("update", name => {
    console.log("Store " + name + " updated!");
    console.log(state.current());
});

store.setState({
    new_key: "value"
});

Stores

stores are views into your state, they trigger updates with these methods

setState(partialState: Object | Immutable.Map)
updateState(updateFn: Function(prevState: Object | Immutable.Map) -> Immutable.Map)
replaceState(nextState: Object | Immutable.Map)

Example

import { Map, fromJS } from "immutable";
import state from "./state";

const store = state.createStore("storeName", {
    key: "value"
});

console.log(store.current()); // Map { key: "value", new_key: "value" }

store.replaceState({
    replaced: true
});
console.log(store.current()); // Map { replaced: true }

store.replaceState({
    replaced: true
});
console.log(store.current()); // Map { replaced: true }

Example Store

import { Map, List } from "immutable";
import State from "@nathanfaucett/state-immutable";

const state = new State();

let ID = 0;

const todos = state.createStore("todos", {
    list: List()
});

todos.create = text => {
    const id = ID++;

    todos.updateState(state =>
        state.update("list", list =>
            list.push(Map({
                id: id,
                text: text
            }))
        );
    );
};

todos.remove = id => {
    todos.updateState(prev => {
        return prev.update("list", list =>
            list.remove(list.findIndex((todo) => todo.get("id") === id))
        );
    });
};

Popularity

Maintenance

Development

Last ver 2 years ago
Created 2 years ago
Last commit 1 second ago
1 second between commits

Technology

Node version: 10.0.0
8.9K unpacked

Compliance

MIT License
OSI Approved
0 vulnerabilities

Contributors

0 contributors

Tags

state
store
immutable
Ready for the next level?
Join Openbase's founding team to help us build the ultimate open-source app store, work with the latest technologies, and enjoy great culture, impact and autonomy
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookLinkedIn
© 2020 Devstore, Inc.