Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Your local, friendly storekeeper. Apothecary is:
It's pure JavaScript and dependency free, so it can be used with React or any other JS UI tools you prefer.
In Flux, data is managed in a centralized data store, and there's a dispatcher that sends messages (usually called actions) to it, causing the store to be updated. The view layer subscribes to the store and updates when the store changes. To implement this scheme, using apothecary consists of the following elements:
These are all described in turn below.
Declare your initial state however you see fit, and pass it into initialize
:
import { initialize } from 'apothecary'
const initialState = { n: 1 }
const store = initialize(initialState)
A mutator is a function that describes a change to our application state. It's usually applied as a result of a user . In apothecary, a mutator is just a function that takes the current state and returns the new state. Dispatching that mutator to the store will cause the store to update. Example:
const increment = state => ({ ...state, n: state.n + 1 })
store.dispatch(increment)
This sends the increment
message to the store which causes the mutation. Assuming that n
was initially equal to
1, we could verify that it was incremented:
const { n } = store.getState() // 2
Conceptually, a mutator takes the ideas of actions and reducers in Redux and combines them into a single, simple abstraction.
Some mutators will need to be asynchronous, like submitting form data to a server. You can use the jam
function to
make your mutators asynchronous. Here's how you might use it:
import { initialize, jam } from 'apothecary'
import api from 'APP/api'
const initialState = { n: 1 }
const increment = state => ({ ...state, n: state.n + 1 })
const incrementAsync = jam(dispatch =>
api.increment().then(() => dispatch(increment))
)
store.dispatch(incrementAsync).then(() =>
...
Notice in the last line above: when we dispatch an async mutator, it returns a promise that we could chain to pop up a toast notification or something similar.
If your state tree gets fairly deep, it's much easier to define a mutator that only works on a small subtree, or even
a leaf, of the application state. If you're using a plain JavaScript object to represent your state, you can do that by
drilling into the application state with the split
function. Simple example:
import { initialize, split } from 'apothecary'
const store = initialize({ n: 1 });
const increment = split(n => n + 1, "n");
const decrement = split(n => n - 1, "n");
Notice that although our state is an object, we are only operating on the variable n
. We accomplish this by passing
a list of state keys into split
after our mutation function. These can go arbitrarily deep:
const store = initialize({ counter: { n: 1 } });
const increment = split(n => n + 1, "counter", "n");
const decrement = split(n => n - 1, "counter", "n");
We subscribe to the store so that we can be notified whenever it mutates. The callback will be executed with the new application state, and subscribers can then do whatever's necessary to update the display. An example in react:
componentWillMount() {
store.subscribe(state => this.setState(state))
}
Now this component will be updated every time a mutator is dispatched to the store.
If you are using React, you can download react-apothecary
to bind to the UI layer. This manages subscriptions to
the store and provides a mechanism to easily inject state and mutators into your components, very similar to how it's
done in react-redux
. Here's a simple and complete example:
import React from "react";
import { initialize, split } from "apothecary";
import { Bridge, tunnel } from "react-apothecary";
const store = initialize({ n: 1 });
const increment = () => split(n => n + 1, "n");
const decrement = () => split(n => n - 1, "n");
function Counter({ n, inc, dec }) {
return (
<div>
<button onClick={dec}>-</button>{n}<button onClick={inc}>+</button>
</div>
);
}
const CounterApp = tunnel(state => ({ n: state.n }), {
inc: increment,
dec: decrement
})(Counter);
export default () => <Bridge store={store}><CounterApp /></Bridge>;
Note the subtle difference between the mutators shown here and the ones that we've seen:
// regular mutator
const increment = split(n => n + 1, "n");
// higher order mutator, or "action creator"
const increment = () => split(n => n + 1, "n");
With react-apothecary
you define functions that return mutators. These are like action creators in redux. This is needed so that they can accept arguments. e.g.
const updateComment = text => split(() => text, "someFormState", "comment");
See the react-apothecary repo for further documentation.
FAQs
Your local friendly storekeeper
The npm package apothecary receives a total of 1 weekly downloads. As such, apothecary popularity was classified as not popular.
We found that apothecary 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.