sextile.state¶
What a service holds while it is running, and the typed keys into it.
What a service opens for the life of the process – an archive, an HTTP client, an index – is held on the application and reached from a page. The framework cannot know what any service puts there, so the store is untyped; a StateKey states the type once, on the key, and carries it to both ends:
DB = StateKey[Connection](“db”)
app.state[DB] = connect(…) # in the lifespan conn = request.state[DB] # in a page, typed Connection
A key’s identity is the key, not its name: two StateKey`s with the same name are different keys and do not collide. The name is for `repr and the message when a key is read that was never written.
- class sextile.state.State[source]¶
Bases:
objectWhat a service holds while it is running, keyed by StateKey.
Writable, and held on the application; a page is given the read-only StateReader view of it instead.
- class sextile.state.StateKey(name)[source]¶
Bases:
Generic[_T]A typed key into a service’s state.
- name¶
What the key is called, used in repr and in the error a missing key raises. It is not how the value is stored: the key’s own identity is, so two keys of the same name are distinct.
- Parameters:
name (
str)
- class sextile.state.StateReader(*args, **kwargs)[source]¶
Bases:
ProtocolThe read-only view of a service’s state that a page is given.
A page reads what the service holds and never writes it: shared state changed from a page would reach every other caller at once. This is that guarantee said in the types – there is no __setitem__ here.