# cool-store

> CoolStore is a simple and powerful state store built on top of ImmerJS and RxJS

Latest version **5.0.0** (published 2021-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install cool-store
pnpm add cool-store
yarn add cool-store
bun add cool-store
```

## 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 | 5.0.0 |
| Published | 2021-07-25 |
| First published | 2020-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 28.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Vien Dinh |
| Maintainers | maxvien |
| Keywords | cool-store, rxjs, immerjs, state-management |

## Links

- npm: https://www.npmjs.com/package/cool-store
- Repository: https://github.com/maxvien/cool-store
- Homepage: https://github.com/maxvien/cool-store#readme
- Issues: https://github.com/maxvien/cool-store/issues
- npm.io page: https://npm.io/package/cool-store

## Dependencies (1)

- [immer](https://npm.io/package/immer.md) ^9.0.5

## 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

- 5.0.0 (latest) — 2021-07-25
- 2.0.2 (next) — 2020-02-06
- 4.0.1 — 2020-12-16
- 4.0.0 — 2020-12-16
- 3.0.3 — 2020-11-08
- 3.0.2 — 2020-10-24
- 3.0.1 — 2020-09-06
- 3.0.0 — 2020-09-06
- 2.2.20 — 2020-02-11
- 2.2.19 — 2020-02-11
- 2.2.18 — 2020-02-11
- 2.2.17 — 2020-02-10
- 2.2.16 — 2020-02-10
- 2.2.15 — 2020-02-10
- 2.2.14 — 2020-02-10
- … 35 more at https://npm.io/package/cool-store/versions

## README

# CoolStore

CoolStore is an immutable state store built on top of [ImmerJS](https://www.npmjs.com/package/immer) and [RxJS](https://www.npmjs.com/package/rxjs).

Hit the **Star** button if you love this project ⭐️

## Installation

```
npm install --save cool-store rxjs
```

## Usage

- [CoolStore](https://github.com/Maxvien/cool-store#how-to-use-coolstore)
- [AsyncCoolStore](https://github.com/Maxvien/cool-store#how-to-use-asynccoolstore)

## Examples

- [How to use CoolStore with React?](https://github.com/Maxvien/cool-store/tree/master/examples/cool-store-react)
- [How to use CoolStore with Angular?](https://github.com/Maxvien/cool-store/tree/master/examples/cool-store-angular)

## How to use `CoolStore`?

#### 1. Create Store

```ts
import { CoolStore } from 'cool-store';

interface User {
  name: string;
  email: string;
}

const initialState: User = {
  name: null,
  email: null,
};

const store = new CoolStore(initialState);
```

#### 2. Set State

```ts
store.set(state => {
  state.name = 'Vien Dinh';
  state.email = 'vien@test.com';
});
```

#### 3. Get State

```ts
const state = store.get();
console.log({ user: state });
```

#### 4. Subscribe State with `store.getChanges()` observable.

```ts
store.getChanges().subscribe(state => {
  console.log({ user: state });
});
```

#### 5. Reset State

```ts
store.reset();
```

### # Advanced `store.set()` method.

#### # Set Entire State

```ts
store.set({
  name: 'Vien Dinh',
  email: 'vien@test.com',
});
```

```ts
store.set(() => ({
  name: 'Vien Dinh',
  email: 'vien@test.com',
}));
```

#### # Set State Properties

```ts
store.set(state => {
  state.name = 'Vien Dinh';
});
```

## How to use `AsyncCoolStore`?

#### 1. Create Store

```ts
import { AsyncCoolStore, AsyncCoolState } from 'cool-store';

interface User {
  name: string;
  email: string;
}

const initialState: AsyncCoolState<User, Error> = {
  loading: false,
  data: null,
  error: null,
};

const store = new AsyncCoolStore(initialState);
```

#### 2. Set State

```ts
function getUser(id: number) {
  store.setLoading();

  fetch('https://jsonplaceholder.typicode.com/users/' + id)
    .then(res => res.json())
    .then(data => store.setData(data))
    .catch(error => store.setError(error));
}

getUser(1); // execute the function
```

#### 3. Subscribe State with `store.getChanges()` observable.

```ts
store.getChanges().subscribe(({ loading, data, error }) => {
  console.log({ loading, data, error });
});
```

You can also use `store.get()`, `store.set()`, `store.reset()` methods with `AsyncCoolStore`.

### # Advanced `store.setData()` method.

#### # Set Entire Data

```ts
store.setData({
  name: 'Vien Dinh',
  email: 'vien@test.com',
});
```

```ts
store.setData(() => ({
  name: 'Vien Dinh',
  email: 'vien@test.com',
}));
```

#### # Set Data Properties

```ts
store.setData(state => {
  state.name = 'Vien Dinh';
});
```

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