class Supplier::Preserving

Cached live Supply factory

class Supplier::Preserving is Supplier { }

This is a factory for live Supply objects, which provides the mechanism for emitting new values onto the supplies, whereby values are kept when no consumer has tapped into the Supply. Any tapping will consume the already stored and future values.

Starting a preserving Supply and consuming its values after it is done:

my $p = Supplier::Preserving.new;
start for ^3 {
    $p.emit($_);
    LAST {
        say done after { now - BEGIN now}s;
        $p.done;
    }
}
sleep 2;
react {
    whenever $p.Supply { $_.say}
    whenever Promise.in(2{ done }
}
say also done after { now - BEGIN now }s

Will output:

done after 0.0638467s
0
1
2
also done after 4.0534119s

Methods

method new

method new()

The Supplier constructor.

Type Graph

Type relations for Supplier::Preserving
perl6-type-graph Supplier::Preserving Supplier::Preserving Supplier Supplier Supplier::Preserving->Supplier Mu Mu Any Any Any->Mu Supplier->Any

Stand-alone image: vector

Routines supplied by class Supplier

Supplier::Preserving inherits from class Supplier, which provides the following routines:

(Supplier) method new

method new()

The Supplier constructor.

(Supplier) method Supply

method Supply(Supplier:D: --> Supply)

This creates a new Supply object to which any values which are emitted on this supplier are passed. This is the factory for all live supplies.

(Supplier) method emit

method emit(Supplier:D: Mu \value)

Sends the given value to all of the taps on all of the supplies created by Supply on this Supplier.

(Supplier) method done

method done(Supplier:D:)

Calls the done callback on all the taps that have one.

my $supplier = Supplier.new;
my $supply   = $supplier.Supply;
$supply.tap(-> $v { say $v }done => { say "no more answers" });
$supplier.emit(42);
$supplier.done;

Will output:

42
no more answers

(Supplier) method quit

multi method quit(Supplier:D: Exception $ex)
multi method quit(Supplier:D: Str() $message)

Calls the quit callback on all the taps that have one, passing the exception to them. If called with a Str the exception will be an X::AdHoc with the supplied message.

This is meant for shutting down a supply with an error.