onDiscard method
Registers disposer to run unless the job ends Done: when it is
cancelled, or fails.
For what the body returns or hands outside; everything else — a lock, a temporary file, a subscription — takes onDispose, or it leaks on the successful path where no test on cancellation will see it. The outcome decides, not the value: a body that ends Done without returning what it registered here disposes of it itself. Returns a function that unregisters it.
The registration goes nowhere with the value: it is settled by the
outcome of the job that made it, so a job that took the value — a
parent awaiting run, a caller holding Job.value — registers its
release itself, on the call, ctx.run(child, discard: ...). A job that
returns nothing hands that nothing over all the same: it ends Done,
and a registration made here never runs. In a branch of runAll the
hand-over is the group's to declare: once the group has handed the
values to the caller this does not run, whatever the branch ends with.
See Choosing the callback on the cleanup page and What a group hands back on the groups page.
Implementation
@override
void Function() onDiscard(FutureOr<void> Function() disposer) =>
addCleanup(disposer, always: false);