lean-state 2.0
A State Manager built for Lean Functional Typescript
Checkout: https://github.com/attack-monkey/Lean-Functional-Typescript
Changes from 1.x
- Syntax changes to add more context to queries
Install
npm i lean-state
Use
import { register } from 'lean-state'
Basics
// setglobal.set'greeting'.with'hello world' // getconsole.log global.get.greeting // get the current and pass to functionglobal.onceconsole.loggreeting // set up a listener...global .listenOn'listener1' .for .subscribeconsole.log'listener1: ' + greeting // make changes and the listener will reactglobal.set'greeting'.with'howdy y\'all'global.set'greeting'.with'wassup'global.set'greeting'.with'g\'day mate' // set up a listener that auto tears down when a condition is no longer metglobal .listenOn'listener2' .for .whilegreeting !== 'stop listening' .subscribeconsole.log'listener2: ' + greeting // make changes and both listeners will reactglobal.set'greeting'.with'good day' // this next change will trigger listener2 to tear down...global.set'greeting'.with'stop listening' // listeners can also be set up to listen, starting from the next change...global .listenOn'listener3' .fromNext .whilegreeting !== 'stop listening' .subscribeconsole.log'listener3: ' + greeting // And more changes...global.set'greeting'.with'heeeey!!!'global.set'greeting'.with'stop listening'
A complex example
Lean-state is a key-value store, so while complex data can be stored at a given key, listeners can only listen to changes at that top-level key.
This isn't really a problem at all - but does change the way you think about listening to changes in data.
If for example a key stores a Record of items - but your app is focused on changes only at a given item - then it's a good idea to also capture which id is being focused on and which id is being changed.
// Set up datasetInitialData // Set up listenerslistenToAllCarChangeslistenToNewFocusCar'1' // Make updatesupdateCar'3'.withupdateCar'1'.with // Change focus car, which re-creates the listenerlistenToNewFocusCar'2' // Make more changesupdateCar'1'.withupdateCar'2'.with