# signal-store

> type-safe store library

Latest version **3.2.0** (published 2023-03-21) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2023-03-21 |
| First published | 2022-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Arjay Elbore |
| Maintainers | aelbore |
| Keywords | signal, store |

## Links

- npm: https://www.npmjs.com/package/signal-store
- npm.io page: https://npm.io/package/signal-store

## Recent versions

- 3.2.0 (latest) — 2023-03-21
- 3.1.0 — 2023-03-18
- 3.0.2 — 2023-03-17
- 3.0.1 — 2023-03-17
- 3.0.0 — 2023-03-13
- 2.0.2 — 2023-03-02
- 2.0.1 — 2023-02-20
- 2.0.0 — 2022-11-08
- 1.2.1 — 2022-11-07
- 1.2.0 — 2022-11-07
- 1.0.1 — 2022-10-20
- 1.0.0 — 2022-10-17
- 0.0.1 — 2022-10-12

## README

# signal-store
type-safe store library

#### Create a Store
```ts
import { createStore } from 'signal-store'

type Product = {
  id?: string
  title?: string
  price?: number
  quantity?: number
}

const store = createStore({
  state: {
    products: signal<Product[]>([
      { id: 'p1', title: 'Gaming Mouse', price: 29.99, quantity: 0 }
    ]),
    success: signal(false)
  }, 
  getters: {
    totalQuantity(state) {
      return state.products.value.reduce((p, c) => {
        return p + c.quantity
      }, 0)
    },
    products(state) {
      return state.products.value
    },
    success(state) {
      return state.success.value
    }
  },
  actions: {
    addToCart({ state, dispatch }, payload: Product) {
      const products = [ ...state.products.value ]
      const index = products.findIndex(c => c.id === payload.id)
      if (index !== -1) {
        products[index].quantity++
      } else {
        products.push({ ...payload, quantity: 1 })
      }
      state.products.value = [ ...products ]
      dispatch('onSuccess', true)
    },
    onSuccess({ state }, payload: boolean) {
      state.success.value = payload
    }
  } 
})

store.addToCart({ 
  id: 'p1', 
  title: 'Gaming Mouse', 
  price: 29.99 
})

const totalQuantity = store.totalQuantity.value // 1
const success = store.success.value // true
```

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