Git Product home page Git Product logo

picostate's Introduction

repo-banner

npm i picostate --save

Event-based immutable state management.

  • 400 bytes
  • familiar event based interface
  • only fires listeners for changed values
  • emits state updates only when requested
  • integrates seamlessly with React

Usage

Create a store:

import createStore from "picostate";

const store = createStore({
  a: 0,
  b: false
});

Safely access state:

store.state; // { a: 0, b: false }

Register a listener for any state changes:

store.listen(state => console.log(state));

Register a listener only for changes to an array of props, in this case b:

store.listen(["b"], state => console.log(state.b));

Update state:

const emit = store.hydrate({ a: 1 });

Fire registered listeners:

emit(); // { a: 1, b: false }

Update state and fire listeners:

store.hydrate({ b: true })(); // { a: 1, b: true }, true

Update state using the previous state:

store.hydrate(state => ({ a: state.a + 1 }))(); // { a: 2, b: true }
const removeListener = store.listen(state => console.log(state));
store.hydrate({ a: 1 })(); // { a: 1, b: true }
removeListener();
store.hydrate({ a: 2 })(); // no listener fired
store.state; // { a: 2, b: true }

Reset to initial state:

store.hydrate();
store.state; // { a: 0, b: false }

License

MIT License © Eric Bailey

picostate's People

Contributors

estrattonbailey avatar dependabot[bot] avatar

Stargazers

Roman avatar Andrejs Agejevs avatar  avatar Andrew Chou avatar Aleksejs Skurjats avatar Carter avatar Jin Yao avatar hunor karamán avatar Jon avatar Kennedy Rose avatar Alex Carpenter avatar Oliver Belmont avatar Matt Coleman avatar Gokul Kathirvel avatar Vladimir Kutepov avatar Kevin Green avatar

Watchers

Nik Rowell avatar Peter Kloss avatar  avatar

Forkers

nikrowell

picostate's Issues

setState() method that automatically emits

Promise I'm not going to bombard you with Issues and PRs 😆

One more pattern I've found myself using with picoapp + picostate is defining a setState function that automatically emits change notifications:

const store = createStore({
  isLoading: false,
  foo: true,
  bar: false
})

function setState(fn) {
  const dispatch = store.hydrate(fn)
  dispatch()
}

function onSubmit(event) {
  event.preventDefault()
  setState({isLoading: true})
  // ...
}

I've found that I almost always want to emit after hydrating state. This feels slightly more clear than store.hydrate(fn)() and is a nice familiar function name 😀

Interesting?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.