JavaScript data structures with immutable state events.
Install
npm install pek
Then:
const pek = ;
Quick Start
To get started, create a data model for your application using the same data
types as you would with JSON (String
, Number
, Boolean
, Object
, Array
, and null
):
const APP_STATE = name: 'Todo List' lists: name: 'Shopping' items: 'Milk' 'Bananas' ;
Next, create a Pēk model by passing this data model to pek()
:
const model = ; // ⇨ { name: 'Todo List', lists: [ { name: 'Shopping', items: [Object] } ] }
That's pretty much all there is to it. Your Pēk model looks and feels just like the original:
modellists0name = 'Zoo Supplies';modellists0items; modellists0; // ⇨ { name: 'Zoo Supplies', items: [ 'Milk', 'Bananas', 'Monkeys' ] }
... with one important difference - you can subscribe to state changes:
const unsubscribe = model__; modelname = 'Pek Example';
Anytime your Pēk model changes, all listeners are notified. Moreover, the state
listeners receive is immutable:
model__; modelname = '\Pek Example 2';
In non-strict mode, changing the state will fail silently, as above. In strict mode, changing the state will throw an exception:
model__; modelname = '\Pek Example 3'; ⇒ Cannot assign to read only property 'name' of object '[object Object]'
API
pek(initialState)
Creates a new Pēk model
initialState
- Object or Array holding your initial model state- Returns a Pēk model (a Proxy-wrapped version of
initialState
)
E.g.
const model = ; // ⇨ { a: 'hello', b: [ 'world' ] }
model. ... __.on(callback)
Listen for changes to model state.
callback
- Function to call any time the target object or any of it's subordinate objects change state. This function takes the following arguments:state
- An immutable copy of the object state. Attempting to modify
- Returns unsubscriber
Function
.
const unsubscribe = model__; modela = 'Hello'; ⇒ a: 'Hello' b: 'world'
To unsubscribe, call the returned unsubscriber Function
:
;
Markdown generated from src/README_js.md by