Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
little-atom
Advanced tools
Readme
State management without the bulk.
...Inspired very heavily by tiny-atom, but not as minimal... get it?
$ yarn add little-atom
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
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)
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 atompayload
- the payload the action was called withonMutation(state, chain)
- a function called after each mutation
state
- current statechain
- 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 runoptions.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 stateatom.mutate(update)
- mutates the state with update
atom.actions
- object of actions available with identical signature as the passed in actions
objectFAQs
State management without the bulk.
The npm package little-atom receives a total of 0 weekly downloads. As such, little-atom popularity was classified as not popular.
We found that little-atom demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.