Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@libre/atom
Advanced tools
Readme
@libre/atom
provides a data type called Atom
s and a few functions for working with Atom
s. It is heavily inspired by atom
s in Clojure(Script). While the full power of Clojure atom
s cannot be experienced in JavaScript's single-threaded runtime, Atom
s do still offer similar benefits due to the highly asynchronous and event-driven nature of JavaScript.
Atoms provide a predictable way to manage state shared by multiple components of a
program as that state changes over time. They are particularly useful in the functional and reactive programming paradigms, where most components of a program are pure functions operating on immutable data. Atoms
provide a controlled mechanism for mutability that lets multiple components access and update the same value without risking mutating another component's reference to it in the middle of some process or asynchronous operation.
Atom
:import { Atom } from "@libre/atom";
const appState = Atom.of({
color: "blue",
userId: 1
});
deref
You can't inspect Atom
state directly, you have to deref
erence it, like this:
import { deref } from "@libre/atom";
const { color } = deref(appState);
swap
You can't modify an Atom
directly. The main way to update state is with swap
. Here's its call signature:
function swap<S>(
atom: Atom<S>,
updateFn: (state: DeepImmutable<S>) => S
): void;
updateFn
is applied to atom
's state and the return value is set as atom
's new state. There are just two simple rules for updateFn
:
To illustrate, here is how we might update appState
's color:
import { swap } from "@libre/atom";
const setColor = color =>
swap(appState, state => ({
...state,
color: color
}));
Note: Our
updateFn
is spreading the old state onto a new object before overridingcolor
. This is an easy way to obey the rules ofupdateFn
. If manually spreading values seems tedious, there are many libraries that offer convenient functions for operating on JS data structures in an immutable manner, e.g. see ramda, sanctuary, crocks, or (for the wizards among us) fp-ts.
NPM: npm install --save @libre/atom
Yarn: yarn add @libre/atom
CDN: <script src="https://unpkg.com/@libre/atom" />
window["@libre/atom"]
ES6 import
import { Atom, deref, set, swap } from "@libre/atom";
CommonJS require
const { Atom, deref, set, swap } = require("@libre/atom");
Web <script />
tag
const { Atom, deref, set, swap } = window["@libre/atom"];
You can find API docs for @libre/atom
here
Please open an issue if you have any questions, suggestions for improvements/features, or want to submit a PR for a bug-fix (please include tests if applicable).
FAQs
Unknown package
The npm package @libre/atom receives a total of 2,145 weekly downloads. As such, @libre/atom popularity was classified as popular.
We found that @libre/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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.