3.2.0 • Published 1 year ago

signal-store v3.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

signal-store

type-safe store library

Create a Store

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
3.2.0

1 year ago

3.0.2

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.1

2 years ago