本页尚未翻译,因此以英文显示。 参与翻译

Sample the gauge on each poll tick

Toss-up async

Requirement

A pressure gauge emits readings 1..8, one every 50 ms. A dashboard polls three times — at 125, 275 and 425 ms — and each poll should show the latest reading at that instant: 2, 5, then 8. Print the three polled readings after the streams close. Both schedules are simulated in the code; both versions must print the lines shown under Expected output.

Expected output
poll 1: reading 2
poll 2: reading 5
poll 3: reading 8

Side by side

RxDart

FxDart

Why they differ

They don't. "The latest value at this instant" only exists in the push model — it presumes things arrive on their own — and both panels express it as the same one-operator sentence: the gauge stream, sampled by the poll stream. RxDart writes gauge().sample(polls()); fxdart writes fxEvents(gauge()).sampleOn(polls()). In both, the "what is current?" state lives inside the operator, each trigger emits the newest reading since the last one, and unsampled readings are simply dropped.

The pull pipelines still have no clock and no "current value" — that refusal stands. Instead, fxdart absorbs the Rx approach in a dedicated events layer: fxEvents is a thin wrapper chain over plain Streams (never an extension, so it collides with nothing) that owns the push-native verbs the pull side would not. RxDart's operator catalog remains far larger; this verb, fxdart speaks natively. And if each sampled reading were the start of real downstream work, .pull() hands the samples to the typed FxAsync pipeline, pulled on demand.

Benchmark

No benchmark for this example. This operator is defined by elapsed time, not by work done: both sides wait out the same windows, so a bar would be measuring Dart's timers rather than either library. Shrinking the windows does not help either — how many values a burst produces would then depend on how fast the machine is, and the two sides could no longer be checked against an identical result. The examples built on timing operators are left unmeasured for that reason; the ones whose delays merely stand in for I/O are benchmarked with those delays set to zero.