@enhance/store

1.0.2 • Public • Published

enhance-store

Single atom reactive data store. Subscribe a function to be called when the data, or a specific key in the store is updated.

Install

npm i @enhance/store

Usage

Subscribe a function to be called anytime the store is updated

import Store from '@enhance/store'

const store = Store({
  one: 1
})
const listener = data => {
  console.log(data.one)
}
store.subscribe(listener)
store.one = 2

Subscribe a function to be called anytime a specific key in the store is updated

import Store from '@enhance/store'

const store = Store({
  one: 1,
  two: 2
})
const listener = data => {
  console.log(data.two)
}

store.subscribe(listener, ['two'])
store.two = 3

Unsubscribe when updates are no longer needed

import Store from '@enhance/store'

const store = Store({
  one: 1
})
const listener = data => {
  console.log(data.one)
}
store.subscribe(listener, ['one'])
store.one = 2
// ...some time later
store.unsubscribe(listener)

⚠️ Most commonly you would subscribe & unsubscribe inside component lifecycle event handlers.

Readme

Keywords

none

Package Sidebar

Install

npm i @enhance/store

Weekly Downloads

11

Version

1.0.2

License

Apache-2.0

Unpacked Size

19.9 kB

Total Files

6

Last publish

Collaborators

  • colepeters
  • ryanbethel
  • brianleroux
  • tbeseda
  • macdonst
  • ryanblock
  • dam