Git Product home page Git Product logo

seal-store's Introduction

seal-store

Build Status version MIT License twitter

Nice Seal Drawing

A simple framework agnostic state store where the state object is immutable by any means other than a setState function.

import initState from 'seal-store'

const { state, setState } = initState({
  saxPlayingSeal: 'looking good',
})

state.saxPlayingSeal = 'looking bad' // => throws TypeError

console.log(state.saxPlayingSeal) // looking good

setState({ saxPlayingSeal: 'looking good but getting dizzy' })

console.log(state.saxPlayingSeal) // looking good but is getting dizzy

Properties of the state object must be defined in the initState and cannot be removed.

const { state, setState } = initState({
  saxPlayingSeal: 'looking good',
})

setState({ fish: 'in water' }) // => throws TypeError

This rule is true infinitely deep.

const { state, setState } = initState({
  secondLevel: { thirdLevel: { fourthLevel: { 'saxPlayingSeal': 'looking good' } } }
})

setState({ secondLevel: { thirdLevel: { fourthLevel: { fish: 'in water' } } } }) // => throws TypeError

There is one exception to adding/removing properties: arrays can shrink or grow. However, in any objects inside of them (other than arrays) properties cannot be added/removed.

const { state, setState } = initState({
  sealsInDocs: [{ name: 'spinningSeal' }],
})

setState({ sealsInReadme: [...state.sealsInReadme, { name: 'happySeal' }] }) // this is fine

setState({ sealsInReadme: [] }) // this is also fine

seal-store also comes with the ability to add a callback when any key in the state is updated.

const stateHasBeenUpdated = (newState) => { console.log('the state has updated') }

const { state, setState } = initState({
  updated: false,
}, stateHasBeenUpdated)

setState({ updated: true }) // => 'the state has been updated'

Unlike other state stores, seal-store doesn't need spread syntax ({ ...items }) for changes that apply to child objects

const { state, setState } = initState({
  secondLevel: {
    updated: false,
    stillHere: true,
  },
})

setState({ secondLevel: { updated: true } })

console.log(state) // => { secondLevel: { updated: true, stillHere: true } }

Install

yarn add seal-store

or

npm add --save seal-store

or as a script tag

<script src="https://unpkg.com/seal-store/dist/seal-store.umd.js"></script>
<script type="application/javascript">
  const { state, setState } = window.sealStore({ 'saxPlayingSeal': 'looking good' })
</script>

proxy Support

seal-store uses proxy objects. If you need to provide IE support, you'll need a proxy polyfill. You can look at the full compatibility table on MDN.

LICENSE

MIT © Ben Williams

seal-store's People

Contributors

biw avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

seal-store's Issues

`null` breaks initState when value is changed

The following throws TypeError: The key 'x' is not in the object '[object Object]':

const initState = require('seal-store')

const { state, setState } = initState({
  video: null,
})

setState({ video: { x: 1, y: { z: 1 } } })

The following version with undefined does not:

const initState = require('seal-store')

const { state, setState } = initState({
  video: undefined,
})

setState({ video: { x: 1, y: { z: 1 } } })

Example: https://runkit.com/719ben/seal-store-null-issue

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.