@lickle/state
v0.0.0-dev.3
Published
Library of reactive state primities
Downloads
26
Readme
Lickle State
A lightweight reactive pub/sub utility for Map
, Set
, and plain objects.
Installation
npm install @lickle/state
# or
yarn add @lickle/state
# or
pnpm add @lickle/state
Usage
Reactive Objects
Wrap any object, Map
, or Set
to make them reactive:
import { reactiveRecord, reactiveMap, reactiveSet, subscribe } from '@lickle/state'
// Reactive object
const state = reactiveRecord({ count: 0 })
subscribe(state, 'count', () => console.log(`Count: ${state.count}`))
state.count += 1 // Logs: Count: 1
// Reactive map
const map = reactiveMap(new Map())
subscribe(map, 'key', () => console.log('Key changed!'))
map.set('key', 'value') // Logs: Key changed!
// Reactive set
const set = reactiveSet(new Set())
subscribe(set, () => console.log('Set updated!'))
set.add('item') // Logs: Set updated!
Batch Updates
Group multiple updates to trigger a single notification:
import { batch } from '@lickle/state'
batch(state, () => {
state.count += 1
state.count += 1
}) // Logs: Count: 2 (once)
Suspend Listeners
Pause notifications while making updates:
import { suspend } from '@lickle/state'
suspend(state, () => {
state.count += 1
}) // No logs
License
MIT © Dan Beaven