Repository navigation
Add peek() to HasEmitters for non-reactive attribute access - #3774
Conversation
|
Performance benchmarks:
|
| CURRENT_COMPUTED = None | ||
|
|
||
| try: | ||
| return getattr(self, name, default) |
There was a problem hiding this comment.
Just curious, but why don't you simply access the private attribute so you don't have to mess with the current computed stuff?
Looking at BaseObservable, the private name is always the public name with a leading underscore. This is also how you were accessing time.
Separate issue: I assume you'll add tests? Once those are in, this can be merged.
There was a problem hiding this comment.
That was actually my first thought too But I realized that directly accessing _{name} is a bit brittle after running some smoke test.
First, it breaks on @computed_property, which stores its internal state under _computed_{name} rather than _{name}.
Even if you correctly guessed the name _computed_speed, grabbing the private variable directly bypasses the reactive engine entirely. When you access a @computed_property via its public name, the framework's descriptor checks if state.is_dirty or if its parents have changed. If dependencies have changed, it explicitly calls state.evaluate() to calculate the fresh value before returning it. If you access the private variable directly, you are just grabbing the internal ComputedState object, and its cached .value might be entirely stale. By using .peek("speed"), you route the request through the public descriptor, which natively executes the is_dirty evaluation logic, while still safely blinding the CURRENT_COMPUTED tracker
There was a problem hiding this comment.
Done with tests!
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Adds a
peekmethod to theHasEmitters. This allows developers to retrieve the value of anObservablewithout registering it in the reactive dependency graph.Motive
In the
mesa_signalsarchitecture, reading anObservablefrom within aComputedStateautomatically logs it as a dependency. For components like we discussed in #3766 , we frequently need to read a state (e.g.,model.time) without permanently binding to it or triggering a reactive signal cascade every time the clock ticks.Implementation
.peek()directly on theHasEmittersmixin.core.CURRENT_COMPUTEDtoNone, extracts the value viagetattr, and securely restores the previous tracker state in afinallyblock.Usage Examples