Watch an Operation to its end How-to
You want to know the moment a durable call ends, without reading its Operation again and again. A watch sends each change of one Operation as it happens, and it closes when the work ends.
Before you start
Read these first if a step below is new to you.
OperationsConcept
Read an OperationHow-to
- Call
operation.watchwith the Operation's identity. Over HTTP, read the answer as a Server-Sent Events stream. A browserEventSourcedoes this for you. - Read the first event. It is the submission of the work.
- Read each later event. Each one is a change of state, or the Operation being held for a person to look at. While nothing changes, a heartbeat arrives, and it needs nothing from you.
- If the connection drops, reconnect with the
Last-Event-IDheader set to the id of the last event you received. The stream continues after that event, and a browserEventSourcesends the header for you. - Read the last event. It states how the work ended, it carries the result, and the stream closes. Where it carries a reference to the result instead of the result itself, read the Operation to get it.
You are done when the stream closes after an event that states how the work ended. If the watch is refused before its first event, no stream opens, and the answer carries the refusal's code. If the stream closes with a refusal code instead, a Mandate you watch under was revoked or your Principal was barred while you watched. If the work had already ended when you opened the stream, you receive its history and its last event at once, and then the stream closes.