Ah, that makes sense and that works because it’s another context block.
The original I thought implied something like this:
#let st1 = state("my-state", "initial")
#context {
st1.update(x => x + "-updated")
// here
st1.get()
}
and here the st1.get() never sees anything other than "initial" for the reasons already stated - the context starts before the update.
In general - lacking knowledge of the bigger problem - I think that working with query and metadata (if needed) is often easier than using state. It might be possible to use query and metadata more extensively for your problem.
Your refsection function already wraps a section of the document doc; this is good for a document structure approach. You shouldn’t need state refsection-id to communicate between refsection and print-bibliography for example, because of this. That’s at least my hunch.
You could even contemplate storing data in the style chain (the way elembic stores its settings) if you want to explore another complicated option…