Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Reactive state manager
Effector is an effective multi store state manager for Javascript apps (React/Vue/Node.js), that allows you to manage data in complex applications without the risk of inflating the monolithic central store, with clear control flow, good type support and high capacity API. Effector supports both TypeScript and Flow type annotations out of the box.
Detailed comparison with other state managers will be added soon
npm install --save effector
Or using yarn
yarn add effector
For Web Framework/Libraries:
Package | Version | Dependencies |
---|---|---|
effector-react | ||
effector-vue |
For another languages:
Package | Version | Dependencies |
---|---|---|
bs-effector | ||
bs-effector-react |
Three following examples that will give you a basic understanding how the state manager works:
import {createStore, createEvent} from 'effector'
import {useStore} from 'effector-react'
const increment = createEvent('increment')
const decrement = createEvent('decrement')
const resetCounter = createEvent('reset counter')
const counter = createStore(0)
.on(increment, state => state + 1)
.on(decrement, state => state - 1)
.reset(resetCounter)
counter.watch(console.log)
const Counter = () => {
const value = useStore(counter)
return (
<>
<div>{value}</div>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<button onClick={resetCounter}>reset</button>
</>
)
}
const App = () => <Counter />
const {createEvent} = require('effector')
const messageEvent = createEvent('message event (optional description)')
messageEvent.watch(text => console.log(`new message: ${text}`))
messageEvent('hello world')
// => new message: hello world
const {createStore, createEvent} = require('effector')
const turnOn = createEvent()
const turnOff = createEvent()
const status = createStore('offline')
.on(turnOn, () => 'online')
.on(turnOff, () => 'offline')
status.watch(newStatus => {
console.log(`status changed: ${newStatus}`)
})
// for store watchs callback invokes immediately
// "status changed: offline"
turnOff() // nothing has changed, callback is not triggered
turnOn() // "status changed: online"
turnOff() // "status changed: offline"
turnOff() // nothing has changed
Event is an intention to change state.
const event = createEvent() // unnamed event
const onMessage = createEvent('message') // named event
const socket = new WebSocket('wss://echo.websocket.org')
socket.onmessage = msg => onMessage(msg)
const data = onMessage.map(msg => msg.data).map(JSON.parse)
// Handle side effects
data.watch(console.log)
Effect is a container for async function. It can be safely used in place of the original async function. The only requirement for function - Should have zero or one argument
const getUser = createEffect('get user').use(params => {
return fetch(`https://example.com/get-user/${params.id}`).then(res =>
res.json(),
)
})
// subscribe to promise resolve
getUser.done.watch(({result, params}) => {
console.log(params) // {id: 1}
console.log(result) // resolved value
})
// subscribe to promise reject (or throw)
getUser.fail.watch(({error, params}) => {
console.error(params) // {id: 1}
console.error(error) // rejected value
})
// you can replace function anytime
getUser.use(() => promiseMock)
// call effect with your params
getUser({id: 1})
const data = await getUser({id: 2}) // handle promise
Store is an object that holds the state tree. There can be multiple stores.
// `getUsers` - is an effect
// `addUser` - is an event
const defaultState = [{ name: Joe }];
const users = createStore(defaultState)
// subscribe store reducers to events
.on(getUsers.done, (oldState, payload) => payload)
.on(addUser, (oldState, payload) => [...oldState, payload]))
// subscribe side-effects
const callback = (newState) => console.log(newState)
users.watch(callback) // `.watch` for a store is triggered immediately: `[{ name: Joe }]`
// `callback` will be triggered each time when `.on` handler returns the new state
Most profit thing of stores is their compositions:
// `.map` accept state of parent store and return new memoized store. No more reselect ;)
const firstUser = users.map(list => list[0])
firstUser.watch(newState => console.log(`first user name: ${newState.name}`)) // "first user name: Joe"
addUser({name: Joseph}) // `firstUser` is not updated
getUsers() // after promise resolve `firstUser` is updated and call all watchers (subscribers)
Domain is a namespace for your events, stores and effects. Domain can subscribe to event, effect, store or nested domain creation with onCreateEvent, onCreateStore, onCreateEffect, onCreateDomain(to handle nested domains) methods.
import {createDomain} from 'effector'
const mainPage = createDomain('main page')
mainPage.onCreateEvent(event => {
console.log('new event: ', event.getType())
})
mainPage.onCreateStore(store => {
console.log('new store: ', store.getState())
})
const mount = mainPage.event('mount')
// => new event: main page/mount
const pageStore = mainPage.store(0)
// => new store: 0
Dmitry 💬 💻 📖 💡 🤔 🚇 ⚠️ | andretshurotshka 💬 💻 📖 📦 ⚠️ | Sergey Sova 📖 💡 | Arutyunyan Artyom 📖 💡 | Ilya 📖 |
---|
effector 0.18.10-0.18.11
store.updates
, representing updates of given store. Use case: watchers, which will not trigger immediately after creation (unlike store.watch
)
import {createStore, is} from 'effector'
const $clicksAmount = createStore(0)
is.event($clicksAmount.updates) // => true
$clicksAmount.watch(amount => {
console.log('will be triggered with current state, immediately, sync', amount)
})
$clicksAmount.updates.watch(amount => {
console.log('will not be triggered unless store value is changed', amount)
})
FAQs
Business logic with ease
The npm package effector receives a total of 42,118 weekly downloads. As such, effector popularity was classified as popular.
We found that effector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.