Learn
Observe individual executions
Distinguish overlapping executions across UseCases and read elapsed intervals without confusing them with CPU time.
Two catalog screens can each own a CatalogUseCase. Both submit execution 1. A Snapshot tells you what each UseCase currently exposes; it does not give a combined record of every attempt. Use UseCaseExecutionObserver when you need to distinguish those attempts or understand where elapsed time went.
Core 1.4.0 adds source identity and timing to the existing observer. It does not change admission, scheduling, cancellation or the five event kinds.
Choose the observation you need
| Question | Tool |
|---|---|
| What State should the UI render now? | Snapshot and the Flutter Builder/Selector |
| Which invocation finished, including overlapping work? | Execution observer |
| How do I assert a particular invocation in a test? | Test 1.2.0 execution collector |
| How do I inspect executions across live UseCases? | DevTools 1.2.0, All executions |
Attach one collector to several UseCases
Implement UseCaseExecutionObserver.onExecutionEvent and pass the collector as executionObserver to each UseCase constructor. The callback receives UseCaseExecutionEvent, not a Command object, State, error or stack trace.
Each runtime event has a stable UseCaseExecutionSource for its UseCase instance. Identify an attempt by Source plus Execution ID:
- two instances of the same type can both have execution 1;
- closing one and creating its replacement produces a new Source;
source.useCaseTypeis a readable label, not identity;- Source is isolate-local, not a distributed trace ID.
A Source does not hold its UseCase, Command, State, key or clock. Old manually constructed events may have no Source: do not merge their IDs into one invented execution.
For a complete example that creates two UseCases, submits work and waits for both results, follow Test overlapping executions. It also demonstrates replacement after close.
Read the events in order
The existing milestones are submitted, queued, processingStarted, rejected and finished. Queuing is not required for every attempt. Rejected Commands have a terminal rejection event but do not enter accepted-work history.
Callbacks are synchronous and best-effort. Keep them short; callback exceptions are contained. The observer is fixed at construction. Disposing a collector does not remove that constructor argument or close its UseCases.
Understand the three intervals
Timing is sampled at lifecycle events; the values do not tick between events.
| Field | What it includes |
|---|---|
beforeProcessing | Submission to processingStarted: input policies, admission, queues, starting hooks and replacement waits |
processing | processingStarted to this event, including awaited work and finalization before finished |
total | Submission to this event |
For a complete finished sample, 110 ms before processing plus 90 ms processing gives 200 ms total. Processing is not CPU time or handler-only time. Observer and diagnostics work can delay later checkpoints; its cost is not subtracted.
Entry.startedAt is recorded before the asynchronous onProcessingRegistryAdd hook. The observer reports processingStarted after that hook succeeds. Subtracting Entry dates therefore does not reproduce these intervals. Rejected timing ends at the rejection decision, before onCommandRejected completes.
Read coverage before interpreting a number
| Status | Meaning |
|---|---|
measured | Submission was observed; available intervals are complete |
partial | Submission was not observed; later intervals may still be known |
unsupportedClock | A custom clock lacks monotonic elapsed-time support |
invalidClock | Elapsed time threw, was negative or moved backwards |
capacityExceeded | The bounded measurement store could not retain this attempt |
Zero is a valid duration. Null is not zero. A measured event with no processing interval has not reached processingStarted; partial or unavailable data does not establish whether processing happened.
observedAt is an optional calendar label from the configured clock. It is not the source of elapsed durations. VM timestampMicros remains a system-calendar timestamp: do not subtract it from virtual-clock dates or use it for cross-isolate elapsed comparisons.
Custom clocks and retention
Existing UseCaseClock implementations still work. To supply timing, additionally implement UseCaseMonotonicClock.elapsed(): return a non-negative, nondecreasing Duration from a stable origin without advancing time or running callbacks. TestUseCaseClock already does this.
Core stores bounded live measurement marks, not an event journal. Terminal delivery releases a mark; close clears remaining marks. Measurement overflow never rejects a Command. Without an observer or active VM diagnostics, no new timing samples, history, periodic timer or elapsed reads are created.
Bound your collector and any Source-to-label map together. The test helper already provides limits. For interactive diagnosis, read All executions. This API is not durable monitoring, event replay, distributed tracing or an execution-control interface.