set-state
Heavily inspired by PureState, set-state
allows simple state management by synchronously updating dependencies and dependants
when state changes.
Getting Started
npm install --save set-state
Example
const state = // Stateful variables are just JS values wrapped with a `state` call.// Since the resulting function acts like a container, you can use constconst x = // This reads a stateful variable; read as "console.log(x)"console // This writes a stateful variable; read as "x = 1" // Stateful variables can depend on other stateful variables const y = const z = consoleconsoleconsole // Those above output "1", "2", "[1, 2, 3]" // If you change a stateful variable, all variables that depend on it are updated. // sets x to 10 consoleconsoleconsole // Now those above output "10", "11", "[10, 11, 21]" // If you use branching logic in a stateful variable, set the dependencies as default// parameters to avoid issues with the dependency tree not updatingconst a = const b = const c = console // yields `2` (correct)console // yields `3` (correct) // To serialize your state, set-state provides a .toJSON methodconst str_prop = const date_prop = JSON // "{"str_prop":"str","date_prop":"1970-01-01T00:00:00.000Z"}" // To listen for changes, use the .on() method which returns the cancel functionconst noise = const cancel = noise // I heard a beep // I heard a boop
API
// async methods // state(b) when b resolves // creates projections of state(a)// all projections are sealed // acts like a cursor for a(), path can be str or arr // state(() => fn(a())) // state(() => [].concat(...a().map(fn)) // alias for flatMap // state(() => [a(), b(), ...c()]) // state(() => b(fn(b(), a()))), b is optional // state(() => b()(a())); applicative is similar to map but b is state(() => fn) // state(() => f(a()) || g(a())) // utility methods // state(a) will no longer update, the value is still a // alias for freeze // state(a) will be read-only but continue to update // statics// static projections are also sealedstate // state(() => [...arr()])state // state(() => {a: a(), b: b()}) // static utility methodsstate // state(a)state // state(a) will no longer update, the value is still astate // alias for freezestate // state(a) will be read-only but continue to updatestate // Booleanstate // Booleanstate // Boolean