1.7.16 • Published 3 years ago

vnstore v1.7.16

Weekly downloads
291
License
MIT
Repository
gitlab
Last release
3 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

3 years ago

1.7.15

3 years ago

1.7.13

4 years ago

1.7.14

4 years ago

1.7.11

4 years ago

1.7.12

4 years ago

1.7.10

4 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.7.7

4 years ago

1.7.6

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.40

5 years ago

1.6.42

5 years ago

1.6.41

5 years ago

1.6.44

5 years ago

1.6.43

5 years ago

1.6.39

5 years ago

1.6.38

5 years ago

1.6.35

5 years ago

1.6.37

5 years ago

1.6.36

5 years ago

1.6.34

5 years ago

1.6.33

5 years ago

1.6.32

5 years ago

1.6.31

5 years ago

1.6.30

5 years ago

1.6.28

5 years ago

1.6.27

5 years ago

1.6.29

5 years ago

1.6.26

5 years ago

1.6.25

5 years ago

1.6.24

5 years ago

1.6.23

5 years ago

1.6.22

5 years ago

1.6.21

5 years ago

1.6.20

5 years ago

1.6.19

5 years ago

1.6.17

5 years ago

1.6.16

5 years ago

1.6.18

5 years ago

1.6.15

5 years ago

1.6.14

5 years ago

1.6.13

5 years ago

1.6.12

5 years ago

1.6.11

5 years ago

1.6.10

5 years ago

1.6.9

5 years ago

1.6.8

5 years ago

1.6.7

5 years ago

1.6.4

5 years ago

1.6.6

5 years ago

1.6.5

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago