# atomicky

> ### Flexible state management

Latest version **1.7.0** (published 2022-12-28) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install atomicky
pnpm add atomicky
yarn add atomicky
bun add atomicky
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.7.0 |
| Published | 2022-12-28 |
| First published | 2021-04-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | m1st1ck |
| Keywords | state, atom, atomicky |

## Links

- npm: https://www.npmjs.com/package/atomicky
- Repository: https://github.com/m1st1ck/atomicky
- Homepage: https://github.com/m1st1ck/atomicky#readme
- Issues: https://github.com/m1st1ck/atomicky/issues
- npm.io page: https://npm.io/package/atomicky

## 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.7.0 (latest) — 2022-12-28
- 1.6.8 — 2022-08-18
- 1.6.4 — 2021-12-30
- 1.6.3 — 2021-10-08
- 1.6.2 — 2021-10-08
- 1.6.1 — 2021-08-27
- 1.6.0 — 2021-08-27
- 1.5.0 — 2021-05-12
- 1.4.2 — 2021-05-03
- 1.4.1 — 2021-05-01
- 1.4.0 — 2021-05-01
- 1.3.0 — 2021-05-01
- 1.2.0 — 2021-04-23
- 1.1.1 — 2021-04-23
- 1.1.0 — 2021-04-23
- … 1 more at https://npm.io/package/atomicky/versions

## README

# Atomicky

### Flexible state management

## Atoms

### atom(state): Atom

```javascript
import { atom } from "atomicky";

// create new atom
const countAtom = atom(0);
// change state
countAtom.setState(2);
// change state with callback function
countAtom.setState((previousCount) => previousCount + 1);

const userAtom = atom({ name: "Stad", age: 2 });
// updating objects directly will merge with previous state
userAtom.setState({ age: 3 }); // { name: "Stad", age: 3 }
// using update function needs to provide the whole object
userAtom.setState((prevState) => ({
  ...prevState,
  age: 4,
}));
// get state
const count = countAtom.getState(); // count === 3
// listen for changes to state
const unsubscribe = countAtom.subscribe(() => {
  // do something with new state
  const count = countAtom.getState();
});
// stop listening for changes
unsubscribe();
// reset to default state
countAtom.reset();
```

### httpAtom(state): Atom

contains an additional http state

```javascript
{
  init: true,
  loading: false,
  loaded: false,
  error: false,
  errorMessage: undefined,
}
```

it provides addition functionallities to manage both states

```javascript
import { httpAtom } from "atomicky";

// create new http atom
const countAtom = httpAtom(0);
// getState returns a tuple both states
const [count, httpState] = countAtom.getState();
// get atom state only
const count = countAtom.getCoreState();
// get http state only
const httpState = countAtom.getHttpState();
// update http state - will override previus state
countAtom.setHttpState({ loading: true }); // { loading: true, init: false, error: false, ... }
// update both state
countAtom.setHttpState({ loaded: true }, 4); // count === 4
// handle errors
countAtom.setHttpState({ error: true, errorMessage: "..." });
// reset - will reset both state and httpState
countAtom.reset(); // ({ init: true, loading: false, ... })
// you can also provide httpState to reset to
countAtom.reset({ loaded: true }); // ({ init: false, loaded: true, ... })
```

## Hooks

### useAtom(Atom): state

```javascript
import { httpAtom, atom, useAtom } from "atomicky";

const nameAtom = httpAtom("Stad");
const countAtom = atom(0);
// listen for state changes and rerender component
const [name, { loading }] = useAtom(nameAtom);
const count = useAtom(countAtom);
```

## Utils

### waitForAtom(atom, selector): Promise

```javascript
import { httpAtom, waitForAtom } from "../src/.";

const userAtom = httpAtom({ name: undefined });

const fetchUser = async () => {
  const { loading } = userAtom.getHttpState();

  // don't fetch if already fetching
  if (loading) {
    // wait for original fetch to finish
    await waitForAtom(userAtom, ([, { loaded }]) => loaded);
    // return fetch data
    return userAtom.getCoreState();
  }

  // Fetch user... and update atom
  userAtom.setHttpState({ loaded: true }, user);
};
```

### waitForAtoms([atom], selector): Promise

```javascript
import { waitForAtoms } from "../src/.";

waitForAtoms(
  [atom1, atom2],
  ([[atom1Data, atom1HttpStatus], [atom2Data, atom2HttpStatus]]) =>
    atom1HttpStatus.loaded || atom1HttpStatus.loaded
).then(() => {});
```

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