Git Product home page Git Product logo

immutator's Introduction

immutator

a redux-inspired state mutator.

npm install immutator

be aware: immutator uses Proxy. see node support and browser support.

example

const immutator = require('immutator')

const state = immutator({ count: 0 })

try {
  state.count++
} catch (err) {
  console.log('Error: state cannot be mutated directly. please define a mutator.')
}

console.log(state) // { count: 0 }

// define mutator `increment`
state.increment = state => state.count++

// call mutator
state.increment() 

console.log(state) // { count: 1 }

state is a proxy whose target can only be mutated by defining and calling a mutator.

functions defined on the immutator are mutators. mutators are passed a mutable copy of state as their first argument when they are called. while they share the same namespace, defining a mutator does not affect state.

api

const immutable_state = immutator(initial_state={})

immutable_state[mutation_type] = mutator(mutable_state, ...mutation_data)

immutable_state[mutation_type](...mutation_data)

immutator's People

Contributors

lukeburns avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

immutator's Issues

play well with morphable

this was made to be used with morphable, but running into some issues with wrapping proxies in proxies.

immutator should wrap morphable (an observable), but morphable internally uses raw to render the initial element that is morphed on state changes. this allows elements to be composed without parent elements reacting to state changes that only child elements depend on.

however, using raw also means that the raw object is vulnerable to mutation within this initial render โ€” also in onload and onunload, where it's probably even more tempting to mutate directly.

i wonder if an ignore function can be introduced into observer-util that would allow me to avoid exposing the raw object.

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.