Socket
Socket
Sign inDemoInstall

little-atom

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    little-atom

State management without the bulk.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
101 kB
Created
Weekly downloads
 

Readme

Source

little-atom

State management without the bulk.

...Inspired very heavily by tiny-atom, but not as minimal... get it?

Usage

$ yarn add little-atom

Example

const createAtom = require('little-atom')

const initialState = { score: 0 }

const actions = {
  increaseScore ({ get, mutate, actions }, points) {
    const { score } = get()
    mutate({ score: score + points })
    actions.checkScore()
  },
  decreaseScore ({ get, mutate, actions }, points) {
    const { score } = get()
    mutate({ score: score - points })
  },
  checkScore ({ get, mutate, actions }, payload) {
    const { score } = get()
    if (score >= 1000) {
      console.log('You win! ...something')
      actions.restart()
    }
  },
  restart ({ get, mutate, actions }, payload) {
    console.log('Restarting')
    mutate(initialState)
  }
}

const onMutation = ({ score }) => {
  console.log(`Your score is ${score}`)
}

const atom = createAtom(initialState, actions, onMutation)

atom.actions.increaseScore(500)
  // -> Your score is 500

atom.actions.decreaseScore(100)
  // -> Your score is 400

atom.actions.increaseScore(600)
  // -> Your score is 1000
  // -> You win! ...something
  // -> Restarting
  // -> Your score is 0

Async Example

nst Preact = require('preact')
const axios = require('axios')
const createAtom = require('little-atom')

const initialState = {
  loading: false,
  topScore: {}
}

const actions = {
  async loadData ({ mutate }, payload) {
    mutate({ loading: true })

    const { data } = await axios.get('/top-score')
    mutate({
      loading: false,
      topScore: data.topScore
    })
  }
}

const atom = createAtom(initialState, actions, render)

const App = ({ loadData, state }) => {
  const { loading, topScore } = state
  if (loading) {
    return <div>Loading...</div>
  } else {
    if (typeof topScore === 'undefined') {
      return <button onclick={loadData}>Get top score</button>
    } else {
      return <div>{`The top score is ${topScore}`}</div>
    }
  }
}

function render (state) {
  Preact.render(<App loadData={atom.actions.loadData} state={state} />, document.body)
}

render(initialState)

API

createAtom(initialState, actions, onMutation, options)

Create an atom.

  • initialState - should be an object, defaults to {}
  • actions - an object of action(atom, payload) functions, keyed by the action name
    • atom - an instance of atom
    • payload - the payload the action was called with
  • onMutation(state, chain) - a function called after each mutation
    • state - current state
    • chain - the chain of actions called to arrive at this mutation (for debugging)
  • options - These are mainly used for debugging purposes
    • options.onAction - called when an action is run
    • options.mutator(state, update) - custom function to mutate state. Defaults to Object.assign({}, state, update)
    • options.get(state) - custom function to return a copy of the current state. Defaults to Object.assign({}, state)

Returns

An instance of atom

  • atom.get() - gets the current state
  • atom.mutate(update) - mutates the state with update
  • atom.actions - object of actions available with identical signature as the passed in actions object

FAQs

Last updated on 18 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc