computed-proxy
Computed properties with JavaScript Proxy.
Examples
const computed = const person = personfirstName = 'Kyle'console // Kyle Robinson Young personfirstName = 'Crystal'console // Crystal Robinson Young
With Arrays
const computed = const restaurant = console // sushi restaurantfoodconsole // sushi, steak
With Nested Properties
const computed = const profile = console // Kyle is 34 years old profilepersonname = 'Crystal'profilepersonage = 33console // Crystal is 33 years old
With Properties Nested in Arrays
const computed = const restaurant = console // sushi costs 50 restaurantfoodconsole // sushi costs 50, steak costs 60 restaurantfood0price = 70console // sushi costs 70, steak costs 60
Volatile
Mark properties as .volatile()
to compute them on every get:
const computed = const person = personfirstName = 'Crystal'console // Crystal Robinson Young
Manually Trigger a Binding
If you want to manually mark a property to recompute on next get use .notifyPropertChange()
:
const computed = const person = console // Kyle Robinson Young personfirstName = 'Crystal'// Since fullName isnt bound to firstName, the get doesn't recomputeconsole // Kyle Robinson Young personconsole // Crystal Robinson Young
Read Only By Default
All properties are read only by default and will throw an error if you try and set them. If you want your property to be settable, provide a set function:
const computed = const person = console // KYLE ROBINSON YOUNG personupperName = 'somebody else'console // SOMEBODY ELSE
Shim
In an environment that doesn't support Proxy
? Shim it by including this in your code:
Similar Projects
license
(c) 2017 Kyle Robinson Young. MIT License