Repository navigation
Conversation
This is the start of computed properties. Computed properties are like functional properties, but they keep track of attribute access. This should then allow us to update them whenever their inputs change. Currently, an error is thrown if anything other than an observable property is accessed.
This will create streams and subscribe to updates on dependencies whenever the property is read. This should mean that we need only read the property once at the start, and we'll then publish updates thereafter. Currently there isn't any global trigger to start listening, and there's no way to shut down the listening coroutines.
The only missing feature is a way of manually triggering a recomputation, which might reintroduce much of the faff I eliminated.
This commit adds a hook to the lifecycle to listen for changes of dependencies and recompute properties. It also makes use of the message broker to close streams and shut down said coroutines.
This adds an async test that checks we get a notification for a computed property when its dependency changes.
This checks that changing dependencies are properly tracked. Note that there's a lingering race condition because of the use of `start_soon`. This should probably be fixed by passing a task status handle to the watching function.
I've eliminated the use of `start_soon` during set-up of computed properties. This should eliminate a potential race condition, where the server starts up before computed properties are fully operational. I suspect this race condition is most likely to be a problem in the test suite, but it's worth eliminating anyway.
This change pushes property subscription earlier in the process: we now make sure there's a subscription to our dependencies before returning the dependency's current value. This should eliminate the race condition where a property update that occurs during recomputation is missed. I've also tightened up the tests significantly, and considered recursion. In tightening up the tests, I've modified the `publish` method to provide a way to trigger recomputation explicitly.
rwb27
marked this pull request as ready for review
October 8, 2026 14:54
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the start of computed properties. Computed properties are like functional properties, but they keep track of attribute access. This should then allow us to update them whenever their inputs change. This uses the same
MessageBrokerthat handles passing notifications to websockets, so there's no extra code required in the properties we're watching.Restrictions
Currently, an error is thrown if anything other than an observable property is accessed by the getter of a computed property. This is intended to reflect the limitations of the current computed property mechanism, but I think it's helpful behaviour. "manual computed properties" for now are just functional properties with the observable flag set to
True. It may be possible in the future to mix computed and manual behaviour, but I don't want to make this PR any more confusing than it already is.Concurrency
LabThings passes messages in the event loop, so we need to run a coroutine to observe our dependencies and recompute as required.
At the moment I think it's probably best to recompute the property in a worker thread, because it will cause confusion to run Thing code in the event loop. That might be a higher-performance alternative we can introduce in the future, but I will not do it now in the interest of doing one thing properly rather than several things confusingly.
We will need to signal the coroutine responsible for triggering recomputation to shut down when the server stops. Currently, that's tricky. This could use the same solution as the MJPEG stream in OpenFlexure. Might be a good time to finally merge that in to this repo.
There's a possible race condition in the current code: if a dependency is added during recomputation, and that dependency changes between the time when it's accessed and the end of the recomputation, the update might get missed. The solution to this is to have the recomputation (which runs in a worker thread) call back to the event loop and subscribe to properties as they are accessed. This isn't yet done.
Before merging: