Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / Live and on-demand supplies / Exercises / Tuning in late

Solution: Tuning in late

Here is a possible solution to the task.

Code

my $supplier = Supplier.new;

my @got;
$supplier.emit(1);
$supplier.Supply.tap(-> $v { @got.push($v) });
$supplier.emit(2);
$supplier.emit(3);

say @got;

🦋 You can find the source code in the file a-live-feed.raku.

Output

[2 3]

Comments

  1. The Supplier is the sending end; its .Supply is what you tap. A live supply broadcasts only to the taps listening at the moment of each emission.

  2. The emit(1) happens before any tap exists, so it is lost — like tuning into a radio station after a song has played. Only 2 and 3, emitted after the tap, are captured, giving [2 3].

Course navigation

Tuning in late   |   Sum a live feed