Skip to content

Commit d73ca23

Browse files
committed
Add some examples to Supply methods documentation
1 parent 7d7d839 commit d73ca23

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

lib/Type/Supply.pod

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,33 @@ terminates that subscription, but keeps the supply as a whole in tact).
6262
Method C<tap> returns an object of type L<Tap|/type/Tap>, on which you can
6363
call the C<close> method to cancle the subscription.
6464
65+
my $s = Supply.interval(1);
66+
my $t = $s.tap(-> $v { say $v }, done => { say "no more ticks" };
67+
sleep 5;
68+
$s.done;
69+
70+
Produces:
71+
72+
0
73+
1
74+
2
75+
3
76+
4
77+
5
78+
no more ticks
79+
80+
6581
=head2 method emit
6682
6783
method emit(Supply:D: \msg)
6884
6985
Sends the C<msg> to all taps (that is, to all subscriptions).
7086
87+
my $s = Supply.new;
88+
$s.tap(&say);
89+
$s.emit(42); # triggers the &say from the previous line
90+
$s.done;
91+
7192
=head2 method done
7293
7394
method done(Supply:D:)
@@ -89,6 +110,11 @@ This is meant for shutting down a supply with an error.
89110
90111
Returns the number of taps.
91112
113+
my $s = Supply.new;
114+
say $s.taps; # 0
115+
$s.tap(&sqrt);
116+
say $s.taps; # 1
117+
92118
=head2 method Channel
93119
94120
method Channel(Supply:D:) returns Channel:D
@@ -105,6 +131,12 @@ Returns a L<Promise|/type/Promise> object for the next value. That means the
105131
promise is kept when there is a next value, and broken when the supply is done
106132
or quit before a value arrived.
107133
134+
my $s = Supply.new;
135+
my $p = $s.Promise;
136+
$p.then(-> $v { say "got $v.result()" });
137+
$s.emit('cha'); # got cha
138+
139+
108140
=head2 method close
109141
110142
method close(Supply:D: Tap:D $t)
@@ -116,12 +148,17 @@ prevents it from getting any more events.
116148
117149
method tappers(Supply:D:) returns List:D
118150
151+
Returns a list of taps as L<Tap|/type/Tap> objects.
152+
119153
=head2 method live
120154
121155
method live(Supply:D:) returns Bool:D
122156
123157
Returns C<True> if the supply is "live", that is, events are emitted to taps
124-
as soon as they arrive. Always returns C<True> in the default C<Supply>.
158+
as soon as they arrive. Always returns C<True> in the default C<Supply> (but
159+
for example on the supply returned from C<Supply.from-list> it's C<False>).
160+
161+
say Supply.new.live; # True
125162
126163
=head2 method wait
127164
@@ -130,6 +167,20 @@ as soon as they arrive. Always returns C<True> in the default C<Supply>.
130167
Waits until the supply is done (in which case it returns C<True>) or C<quit>
131168
(in which case it will throw the exception that was passed to C<quit>).
132169
170+
my $s = Supply.new;
171+
$s.tap(&say);
172+
my $p = Promise.allof((^10).pick(*).map: -> $v {
173+
start {
174+
$s.emit($v)
175+
}
176+
});
177+
$p.then({ $s.done });
178+
179+
$s.wait;
180+
say "Done";
181+
await $p;
182+
183+
133184
=head2 method list
134185
135186
method list(Supply:D:) returns List:D

0 commit comments

Comments
 (0)