Skip to content

Computed properties - #406

Draft
rwb27 wants to merge 4 commits into
mainfrom
computed-properties
Draft

rwb27 wants to merge 4 commits into
mainfrom
computed-properties

Conversation

@rwb27

@rwb27 rwb27 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

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.

Once this is better-tested, we need logic to:

  • Call the computed property at the right time, to initialise its dependencies.
  • Automatically recompute it when those dependencies change.
  • Clean everything up so the server shuts down nicely.

My current thinking is that we'll use the MessageBroker to subscribe to changes in our dependencies. That way, there's no need to add any more notification logic.

Using the message broker will require us to have a coroutine running that receives from a stream and recomputes the property. 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.

There are two concurrency challenges here:

  • if a dependency changes while the property is being recomputed, we might miss it. That's hard to fix without blocking the event loop (or at least blocking notifications of events), which we don't want to do.
  • we would 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.

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.
@rwb27 rwb27 changed the title Functional properties that track attribute access Computed properties Sep 14, 2026
@barecheck

barecheck Bot commented Sep 14, 2026

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 97.42%

Your code coverage diff: -0.01% ▾

Uncovered files and lines
FileLines
src/labthings_fastapi/computed_properties.py53

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant