until a better name can be decided upon
A small library to be used with Mitrhil and Mithril stream(), to prevent calling a component's view()
except when warranted.
The API is dead simple, a single function call to instantiate values, and to add an onbeforeupdate()
function.
const component = {
oninit : (vnode) => {
vnode.state.value = observer(vnode, "my value");
},
view () =>
// renderValue() is slow and processor intensive, so only call when warranted
m("div", renderValue(vnode.state.value()))
}
Some background on Mithril rendering: By default Mithril will call a component's view()
for every redraw, either when triggered by certain event handlers, or by calling m.redraw()
directly. The vdom output from the view()
is then diffed against to the actual DOM and updates are done as necessary. Read more about Mithril and auto-redraw
A component's view()
should be dead simple, and computed values should be handled/cached outside of the view()
. So when does it make sense to use this? TBH I'm not really sure...
A component can opt-out of calling it's view()
by using a lifecycle method - onbeforeupdate()
. When this method returns false
, the view()
will not be called.
Mithril Observer does 2 things:
- Adds
onbeforeupdate
to the component (the component vnode is first argument). - Creates and returns streams from from additional arguments.