sampleOn
Emits the newest source value each time the trigger stream fires — the source sets the values, the trigger sets the rhythm.
Lecture
A sensor updates two hundred times a second; the display repaints
sixty. Processing every update is wasted work — what the consumer
actually wants is the newest value, on its own schedule.
sampleOn(trigger) is exactly that split: the source stream
supplies values, the trigger stream supplies moments, and each trigger
event emits the latest source value.
Two details keep it honest. A trigger that fires when nothing new has arrived stays silent — you never see the same value twice in a row just because the clock ticked. And values that were superseded between triggers are dropped, not queued: this is a lossy operator by design, for state-like streams where only the newest reading matters.
Lifetime follows the source: when the source closes, the chain closes
and the trigger subscription is cancelled — an endless
Stream.periodic tick makes a perfectly good trigger.
Compare the neighbors: throttle
rate-limits with a fixed window measured from the source's own events;
sampleOn hands the schedule to a second stream entirely.
fxdart events layer, after Rx's sample.
Demo 1 · The trigger sets the rhythm
Demo 2 · Silent when nothing is new, closes with the source
Try it yourself
Exercise: one drag position per frame.
throttle — rate limiting from the source's own timing ·
debounce — waiting for quiet instead of sampling ·
withLatestFrom — the same "latest value" idea, but combining two data streams