# @libre/atom

Latest version **1.3.3** (published 2019-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @libre/atom
pnpm add @libre/atom
yarn add @libre/atom
bun add @libre/atom
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2019-11-21 |
| First published | 2018-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 62.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Derrick Beining |
| Maintainers | derrickbeining |
| Keywords | atom, clojure, clojurescript, functional programming, functional-reactive programming, rx, state-management |

## Links

- npm: https://www.npmjs.com/package/@libre/atom
- Repository: https://github.com/libre-org/atom
- Homepage: https://libre-org.github.io/atom/
- Issues: https://github.com/libre-org/atom/issues
- npm.io page: https://npm.io/package/@libre/atom

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.3.3 (latest) — 2019-11-21
- 1.3.2 — 2019-03-30
- 1.3.1 — 2019-02-22
- 1.3.0 — 2019-02-05
- 1.2.2 — 2019-02-04
- 1.2.1 — 2019-02-04
- 1.2.0 — 2019-02-04
- 1.1.0 — 2018-12-11
- 1.0.2 — 2018-12-10
- 1.0.1 — 2018-12-09
- 1.0.0 — 2018-12-09

## README

<div>
  <p align="center">
    <img 
      src="https://document-export.canva.com/DADLRIBWTFM/45/preview/0001-645846858.png"
      height="350"
      width="350"
      alt="@libre/atom logo" />
  </p>
</div>

<h3 align="center">A way to manage shared, synchronous, independent state</h3>

<h3 align="center">Inspired by <a href="https://clojure.org/reference/atoms">atom</a>s in <a href="https://clojure.org/index">Clojure(Script)</a></h3>

<div style="heigth:40px;">&nbsp;</div>

[![TypeScript](https://badges.frapsoft.com/typescript/version/typescript-next.svg?v=101)](https://github.com/ellerbrock/typescript-badges/)
[![npm (scoped)](https://img.shields.io/npm/v/@libre/atom.svg)](https://www.npmjs.com/package/@libre/atom)
[![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/@libre/atom.svg)](https://bundlephobia.com/result?p=@libre/atom)
[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@libre/atom.svg)](https://bundlephobia.com/result?p=@libre/atom)

[![Build Status](https://travis-ci.com/libre-org/atom.svg?branch=master)](https://travis-ci.com/libre-org/atom)
[![codecov](https://codecov.io/gh/libre-org/atom/branch/master/graph/badge.svg)](https://codecov.io/gh/libre-org/atom)
[![npm](https://img.shields.io/npm/dt/@libre/atom.svg)](https://www.npmjs.com/package/@libre/atom)

[![NpmLicense](https://img.shields.io/npm/l/@libre/atom.svg)](https://www.npmjs.com/package/@libre/atom)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

## Description

`@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.

### Put your state in an `Atom`:

```ts
import { Atom } from "@libre/atom";

const appState = Atom.of({
  color: "blue",
  userId: 1
});
```

### Read state with `deref`

You can't inspect `Atom` state directly, you have to `deref`erence it, like this:

```js
import { deref } from "@libre/atom";

const { color } = deref(appState);
```

### Update state with `swap`

You can't modify an `Atom` directly. The main way to update state is with `swap`. Here's its call signature:

```ts
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`:

1. it must return a value of the same type/interface as the previous state
2. it must not mutate the previous state

To illustrate, here is how we might update `appState`'s color:

```js
import { swap } from "@libre/atom";

const setColor = color =>
  swap(appState, state => ({
    ...state,
    color: color
  }));
```

> **Note:** Our `updateFn` is [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)ing the old state onto a new object before overriding `color`. This is an easy way to obey the rules of `updateFn`. 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](https://ramdajs.com/), [sanctuary](https://sanctuary.js.org/), [crocks](https://evilsoft.github.io/crocks/docs/), or (for the wizards among us) [fp-ts](https://gcanti.github.io/fp-ts/).

## Installation

**NPM**: `npm install --save @libre/atom`

**Yarn**: `yarn add @libre/atom`

**CDN**: `<script src="https://unpkg.com/@libre/atom" />`

- Exposed on the global object, like so: `window["@libre/atom"]`

## Usage

**ES6 `import`**

```js
import { Atom, deref, set, swap } from "@libre/atom";
```

**CommonJS `require`**

```js
const { Atom, deref, set, swap } = require("@libre/atom");
```

**Web `<script />` tag**

```js
const { Atom, deref, set, swap } = window["@libre/atom"];
```

## Documentation

[You can find API docs for `@libre/atom` here](https://libre-org.github.io/atom/)

## Contributing / Feedback

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).

---
_Source: https://npm.io/package/@libre/atom · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
