Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / Live and on-demand supplies
On-demand supplies
An on-demand supply produces its values anew for every tap.
Supply.from-list is on-demand: each tap gets the
whole sequence from the start, independently of any
other tap.
my $s = Supply.from-list(1, 2, 3);
my @a;
my @b;
$s.tap(-> $v { @a.push($v) });
$s.tap(-> $v { @b.push($v) });
say @a; # [1 2 3]
say @b; # [1 2 3]Both taps received all three values. The supply did not “use up” its values on the first tap; it simply ran again for the second. This is the behaviour you want when a supply represents a fixed source — a list, a file, a query — that every subscriber should see in full.
On-demand is the default for supplies built from existing data, because each consumer naturally expects the complete sequence. The other kind, a live supply, behaves quite differently, as the next topic shows.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Live and on-demand supplies | Quiz — Supply kinds →
💪 Or jump directly to the exercises in this
section.