Mazzard
Install
npm i mazzard
Next, we will use mazzard
which means we imported it from mazzard
.
// undefined
Unexpected arguments
mazzard
returns the first argument as is, if it is not function
, array
or simple object
.
// 1 // '1' // false // null // NaN // Symbol('test') // instance of Map
Observable object
You get the observable object if the first argument is simple object
.
const observable = console// > 'success'
All objects inside observable are observable.
const test = test // is observable objecttestobservableField // is observable object
Observer
To have reactions on changes of observable object, use mazzard
with a function as the first argument.
The function is observer and runs immediately.
const test = // > undefined testtestField = 'success'// > 'success'
Observer runs each time when you change observable fields.
You may stop the watching with the first argument of observer.
const test = // > undefined testtestField = 'success'// > 'success' testtestField = true// > true teststop = 'test message'// > 'stop', 'test message' testtestField = 'test'// nothing happens
Also, you may stop it with that mazzard returns.
const stop =
If you set the same value which a field have then reaction will not be called.
const test = // > undefined testtestField = true// > true testtestField = true// nothing happens
United changes
If you wanna have only one reaction of observer on several changes, you may use a method of observable object.
const test = // > undefined, undefined testfield1 = 'field1'// > 'field1', undefined testfield2 = 'field2'// > 'field1', 'field2' test// > 1, 2
You may unite changes with action
from mazzard
.
const test = const update = // > undefined, undefined // > 1, 2
The same will happen for setters.
const test = // > undefined, undefined testfullName = 'Mike Mighty'// > 'Mike', 'Mighty'
Computed value
Use getters in observable to have computed value with caching.
const test = // > null testname = 'Mike'// nothing happens testsecondName = 'Mighty'// > 'Mike Mighty'
Observable array
You get the observable array if the first argument is array
.
// observable array
All changes on the array make reactions.