1.7.16 • Published 2 years ago

vnstore v1.7.16

Weekly downloads
291
License
MIT
Repository
gitlab
Last release
2 years ago

vnstore

A lightweight library to manage state + listen to state changes globally

TODO

  • split react stuff into it's own repo
  • combine vnstore + vnlocal. store just has a persist option
  • fix typings on the react stuff
    • also in general need to just solidify the API there

Getting Started

npm i -s vnstore
import {vnstore} from 'vnstore'

const $items = vnstore()

vncontext

import {vncontext, useContextHost, useProp} from 'vnstore'

const ctx = vncontext({count: 0})

const Parent = () => {
  const {host, setProp} = useContextHost(ctx)

  return host(
    <div>
      <Child />
    </div>
  )
}

const Child = () => {
  const setProp = useSetProp(ctx)
  const count = useProp(ctx, 'count')

  return (
    <div>
      <span>{count}</span>
      <button onClick={() => setProp('count', count + 1)} />
    </div>
  )
}

vnstore

Store Config

vnstore({
    fetcher: () => [], // a function which will return a list of items (either sync or as a promise) to fetch a list of items
    dependsOn: [], // a list of stores that when the selection changes will cause this store to be refreshed
    lazy: true, // call the fetcher one when items are needed
})

/** fetcher example */

const $items = vnstore({
    fetcher: () => fetchItemsAsync()
})
$items.refresh() // this will call fetchItemsAsync() and populate the store with it's response

/** lazy example */

const $items = vnstore({
    fetcher: () => fetchItemsAsync(),
    lazy: true
})
// fetchItemsAsync() not called yet
$items.list$(...)
// fetchItemsAsync() is called now


/** dependsOn example */

const $items = vnstore()

const $childrenItems = vnstore({
    fetcher: () => fetchChildrenAsync(),
    dependsOn: [$items]
})

$items.select('a')
// fetchChildrenAsync() is called

$items.select('b')
// fetchChildrenAsync() is called again

Data fetching

$items.loading() // false
$items.loading$(isLoading => ...)
$items.useLoading() // false

$items.refresh()
$items.loading() // true

Setters

$items.insert({id: 'a', prop1: 'abc', prop2: 'def'})
$items.insert([
  {id: 'a', prop1: 'abc', prop2: 'def'},
  {id: 'b', prop1: 'nice', prop2: 'one'}
])
$items.update('a', {prop1: 'ABC'})
$items.delete('a')
$items.select('a')
$items.size() // number

Getters

$items.list() // Item[]
$items.list(e => e.prop1 === 'qwerty') // Item[]
$items.map() // Map<string,Item>
$items.get('a') // Item | undefined
$items.selected() // Item | undefined

Listeners

$items.list$(items => /* Item[] */)
$items.list$(e => e.prop1 === 'qwerty', items => /* Item[] */)
$items.map$(e => /* Map<string,Item> */)
$items.get$('a', e => /* Item | undefined */)
$items.selected$(e => /* Item | undefined */)

React hooks

Hooks use listeners and will rerender component when items change

$items.useList() // Item[]
$items.useList(e => e.prop1 === 'qwerty') // Item[]
$items.useMap() // Map<string,Item>
$items.useGet('a') // Item | undefined
$items.useSelected() // Item | undefined

Other

$items.reset()
$items.list() // []
1.7.16

2 years ago

1.7.15

2 years ago

1.7.13

3 years ago

1.7.14

3 years ago

1.7.11

3 years ago

1.7.12

3 years ago

1.7.10

3 years ago

1.7.9

3 years ago

1.7.8

3 years ago

1.7.7

3 years ago

1.7.6

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.40

3 years ago

1.6.42

3 years ago

1.6.41

3 years ago

1.6.44

3 years ago

1.6.43

3 years ago

1.6.39

3 years ago

1.6.38

3 years ago

1.6.35

3 years ago

1.6.37

3 years ago

1.6.36

3 years ago

1.6.34

3 years ago

1.6.33

3 years ago

1.6.32

3 years ago

1.6.31

3 years ago

1.6.30

3 years ago

1.6.28

3 years ago

1.6.27

3 years ago

1.6.29

3 years ago

1.6.26

3 years ago

1.6.25

3 years ago

1.6.24

3 years ago

1.6.23

3 years ago

1.6.22

3 years ago

1.6.21

3 years ago

1.6.20

3 years ago

1.6.19

3 years ago

1.6.17

3 years ago

1.6.16

3 years ago

1.6.18

3 years ago

1.6.15

3 years ago

1.6.14

3 years ago

1.6.13

3 years ago

1.6.12

3 years ago

1.6.11

3 years ago

1.6.10

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.4

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago