brisky-struct
An observable data structure
- Deep memory efficient prototypes
- Every value is observable
- Serializable references
- Fast reactive state management. Inspired by virtual-dom tree-diffing algorithms and merkle-trees
- Powerful query syntax
- Fast emitters
- Async helpers, work with generators, promises and iterators
- Low footprint (6kb gzipped)
CRUD
Create
const struct = const root = struct
Serialize
root // → { "firstKey": "value" }
Set
⚠ Default behaviour is merge.
rootroot // → { "firstKey": "value", "second": { "subKey": "subValue" } }
Get
root // → { "subKey": "subValue" }
Keys
root // → [ "firstKey", "second" ]
Remove
rootroot // → undefinedroot // → [ "second" ]
Compute
const sub = rootsub // → "subValue"
Navigate
Key
subkey // → "subKey"
Path
sub // → ["second", "subKey"]
Parent
subparentkey // → "second"subparent // → { "subKey": "subValue" }
Root
sub // → { "second": { "subKey": "subValue" } }sub === root // → true
Listen
On
Default listener
var results = rootrootresults // → [ { "third": 3 } ]
Data listener
rootrootresults // → [ { "third": 3 }, { "fourth": 4 } ]
Named data listener
⚠ Only named listeners won't override existing listeners. Notice that fifth
appears twice in the results array.
rootrootresults // → [ { "third": 3 }, { "fourth": 4 }, { "fifth": 5 }, { "fifth": 5 } ]
On as a method
results = const third = rootthirdthirdresults // → [ "changed" ]rootresults // → [ "changed", "again" ]
Once
Once as a method
results = const fourth = rootfourthfourthresults // → [ ]fourthresults // → [ "four" ]
Once as a promise
results = fourthfourthresults // → [ "changed" ]fourthresults // → [ "changed" ]
Emit
⚠ Events fired on a path can be listened only at exact same path.
const errors = rootrooterrors // → [ "satellites are not aligned" ]subsuberrors // → [ "satellites are not aligned", "splines are not reticulated" ]
More about get and set
Get with set
Second parameter of get is a default value for the path.
⚠ It'll be set
and returned in absence of given path otherwise it'll be ignored.
root // → "newValue"root // → "newValue"root // → 5
Get a path with methods
⚠ Available methods are root
, parent
and compute
.
root // → "newValue"root // → { "subKey": "subValue" }sub // → 5
Set without merge (reset)
Third parameter of set is a reset flag.
⚠ Second parameter is a stamp, will come to our plate on further chapters.
const second = rootsecondsecond // → { "subKey": "subValue", "newSubKey": "newSubValue" }secondsecond // → { "onlySubKey": "onlySubValue" }
Master and branches
const master = struct const branchM = master const branchJ = master master // → undefined branchM// → { "year": 2004, "imdb": 7.5, "title": "The Edukators", "favourite": true }branchJ// → { "year": 2004, "imdb": 7.5, "title": "The Edukators" }master// → { "year": 2004, "imdb": 7.5, "title": "The Edukators" } masterbranchJ // → "R"branchM // → "R" branchJbranchM // → "R" masterbranchM // → "PG"branchJ // → "G"