3 ms·
y = Computed(lambda: calculate_y(x())) How does this instance of Computed know that it depends on x? Does it parse the bytecode for the lambda? Does it call th
by twic 1y ago
y = Computed(lambda: calculate_y(x()))
How does this instance of Computed know that it depends on x? Does it parse the bytecode for the lambda? Does it call the lambda and observe which signals get accessed?
In my homebrew signal framework, which emerged in the middle of a complicated web dashboard, this would look like:
y = Computed([x], calculate_y)
So the machinery gets to see the signal directly.
- buibuibui 1y agoI am using the standard Python library `contextvars.ContextVar` as the foundation of my reactivity system's dependency tracking mechanism. In the computation step, when Signals get accessed, I track them as dependencies.
- TOGoS 1y agoI've used systems that did this (some TypeScript TUI library comes to mind) and was similarly confused. I think what actually happened was that the x function/getter/whatever had some 'magic' in it that let it communicate with `Computed` as a side-effect of `Computed` computing the value. Too magical for me. I'd rather have something like you described where inputs are explicit, so I don't have to guess about whether the magic will work in any given case.
- TeeMassive 1y agoThe module probably has its own global register and ever time Computed() is called it adds to it.