New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

little-atom

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

little-atom

State management without the bulk.

  • 0.2.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
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

Package last updated on 18 May 2017

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc