Options
All
  • Public
  • Public/Protected
  • All
Menu

@bytesoftio/store

Installation

yarn add @bytesoftio/store or npm install @bytesoftio/store

Table of contents

Description

Similar to the @bytesoftio/value package, except this one is optimized for use with objects. It comes with differs, mergers and can be used with a custom mapper / connector. Thanks to the @bytesoftio/use-store package, this library allows you to write stores that can be used in any environment as well as inside React through hooks.

createStore

Creates a new instance of ObservableStore .

import { createStore } from "@bytesoftio/store"

// create a new store from initial state
const store = createStore({some: "data"})

ObservableStore

A very simple observable like object.

import { createStore } from "@bytesoftio/store"

const store = createStore({firstName: "John", lastName: "Doe"})

// get the underlying store value, read only
store.get()

// update all of the store data
store.set({firstName: "Steve", lastName: "Jobs"})

// update some of the store data
store.add({lastName: "Wozniak"})

// reset store back to initial state {firstName: "John", lastName: "Doe"}
store.reset()

// reset store state and change its initial value
store.reset({firstName: "James", lastName: "Bond"})

// listen to state changes outside
store.listen(state => console.log(state))

Usage in React

To learn how to use this package inside React, please refer to the @bytesoftio/use-store package.

Index

Type aliases

CreateStore

CreateStore: <TValue>(initialValue: TValue) => ObservableStore<TValue>

Type declaration

    • Type parameters

      • TValue: object

      Parameters

      • initialValue: TValue

      Returns ObservableStore<TValue>

StoreCallback

StoreCallback<TValue>: (newValue: TValue) => void

Type parameters

  • TValue: object

Type declaration

    • (newValue: TValue): void
    • Parameters

      • newValue: TValue

      Returns void

StoreCallbackUnsubscribe

StoreCallbackUnsubscribe: () => void

Type declaration

    • (): void
    • Returns void

StoreDiffer

StoreDiffer<TValue>: (oldValue: TValue, newValue: TValue) => boolean

Type parameters

  • TValue: object

Type declaration

    • (oldValue: TValue, newValue: TValue): boolean
    • Parameters

      • oldValue: TValue
      • newValue: TValue

      Returns boolean

StoreMapper

StoreMapper<TValue, TValueMapped>: (value: TValue) => TValueMapped

Type parameters

  • TValue: object

  • TValueMapped: object

Type declaration

    • (value: TValue): TValueMapped
    • Parameters

      • value: TValue

      Returns TValueMapped

StoreMerger

StoreMerger<TValue>: (oldValue: TValue, newValue: Partial<TValue>) => TValue

Type parameters

  • TValue: object

Type declaration

    • (oldValue: TValue, newValue: Partial<TValue>): TValue
    • Parameters

      • oldValue: TValue
      • newValue: Partial<TValue>

      Returns TValue

Functions

Const createStore

  • createStore(initialValue: TValue): Store<TValue>