So we end the new year with Matrix learning a new trick! The CLJC version so far, with CLJD and JS to follow. Long story short, Matrix got applied to a new application, and new applications tend to require new capabilities. This in turn is because we do not teach Matrix new tricks speculatively, the exception being lazy cells which we knocked off just to address concerns over eager propagation, the MX default. We digress. The new trick?
A formula that still consults one or more dependencies during a given evaluation can signal, as an application decision, that it has reached a final result and can be optimized away, despite the lingering dependencies. This matters greatly in cases where the lingering dependencies would bog down reactive processing by unnecessarily rerunning formulas.
Note that MX internals will on their own optimize away a formulaic cell that, when evaluated initially, happens not to access any other reactive value. But if the formula reads a reactive value, it will remain reactive forever, unless the formula does a weird early branch off a random number or sth. Until now.
The idea here is that the application formula may look at reactive value(s) and decide it is "done". A silly example:
:answer (cF (let [result (* 2 (mget me :factor))]
(if (= result 42)
(cf-freeze result)
result)))
When result reaches 42, it will have read the :factor property and re-established that dependency, but the new cf-freeze mechanism will be recognized as a sign-off, and the :answer formula will be optimized away.
Rest assured, the real example is more compelling but proprietary, yet I am too tired just now to conjure up sth more convincing. 🙂
Fun note: one of the two tests around this problem actually solves the problem without leveraging cf-freeze, but it is a bit hacky closing over an atom that serves as non-reactive but variable state, and it does require one final execution during which the formula will touch no reactive values and get optimized away normally. Here are the tests: https://github.com/kennytilton/matrix/blob/main/cljc/matrix/test/tiltontec/cell/opti_freeze.cljc
Finally...Happy New Year! 🎉