In Concurrency§
See primary documentation in context for whenever
The whenever
keyword can be used in supply blocks or in react blocks. From the 6.d version, it needs to be used within the lexical scope of them. It introduces a block of code that will be run when prompted by an asynchronous event that it specifies - that could be a Supply
, a Channel
, a Promise
or an Iterable
.
Please note that one should keep the code inside the whenever
as small as possible, as only one whenever
block will be executed at any time. One can use a start
block inside the whenever
block to run longer running code.
In this example we are watching two supplies.
my $bread-supplier = Supplier.new; my $vegetable-supplier = Supplier.new; my $supply = supply { whenever $bread-supplier.Supply { emit("We've got bread: " ~ $_); }; whenever $vegetable-supplier.Supply { emit("We've got a vegetable: " ~ $_); }; } $supply.tap( -> $v { say "$v" }); $vegetable-supplier.emit("Radish"); # OUTPUT: «We've got a vegetable: Radish» $bread-supplier.emit("Thick sliced"); # OUTPUT: «We've got bread: Thick sliced» $vegetable-supplier.emit("Lettuce"); # OUTPUT: «We've got a vegetable: Lettuce»