I have a complex network of derivations that runs great (thanks MobX!) but I'd like to log value changes with respect to the upstream values that caused the change, i.e. hierarchically or as a tree.
Before using Mobx, the computed functions were 'update' functions. I could construct an isomorphic log by relying on the call stack and pushing and popping contexts. (In most cases it was safe to assume that if an update was running there was a difference to log but in many cases a previous value was cached to compare against). e.g.
updateA(){
pushLog("A") // creates "A" log node.
if ... updateX()
else ... updateB();
popLog()
}
updateB(){
pushLog("B") // creates "B" log node.
updateC()
if ... updateX()
popLog(); // Remove the B log node, assuming everything else is pushing and popping as it should.
}
updateC(){
pushLog("C")
//...
popLog();
}
updateX(){
pushLog("X")
//...
popLog();
}
With computeds and reactions though there is no clean call stack, so I'm not sure how to properly construct an isomorphic log tree.
I can create reactions for each computed but they will of course run at the end and without a guaranteed order and sans any kind of info on the chain-of-causation so to say.
It smells like I might be able to assemble the log tree after the fact if I kept track of the computed's debug names and then do some kind of dependency lookup in a reaction before appending the log entry — see if a previous entry in the log is a dependency, and if so, append the new log entry to that previous entry. But the log itself is an observable and updating it after the transaction will create other complications — ideally the transaction that triggers the computed would contain the changes to the log.
from Hierarchical logging of MobX derivations/computeds

No comments:
Post a Comment