@@ -62,12 +62,33 @@ terminates that subscription, but keeps the supply as a whole in tact).
62
62
Method C < tap > returns an object of type L < Tap|/type/Tap > , on which you can
63
63
call the C < close > method to cancle the subscription.
64
64
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
+
65
81
= head2 method emit
66
82
67
83
method emit(Supply:D: \msg)
68
84
69
85
Sends the C < msg > to all taps (that is, to all subscriptions).
70
86
87
+ my $s = Supply.new;
88
+ $s.tap(&say);
89
+ $s.emit(42); # triggers the &say from the previous line
90
+ $s.done;
91
+
71
92
= head2 method done
72
93
73
94
method done(Supply:D:)
@@ -89,6 +110,11 @@ This is meant for shutting down a supply with an error.
89
110
90
111
Returns the number of taps.
91
112
113
+ my $s = Supply.new;
114
+ say $s.taps; # 0
115
+ $s.tap(&sqrt);
116
+ say $s.taps; # 1
117
+
92
118
= head2 method Channel
93
119
94
120
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
105
131
promise is kept when there is a next value, and broken when the supply is done
106
132
or quit before a value arrived.
107
133
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
+
108
140
= head2 method close
109
141
110
142
method close(Supply:D: Tap:D $t)
@@ -116,12 +148,17 @@ prevents it from getting any more events.
116
148
117
149
method tappers(Supply:D:) returns List:D
118
150
151
+ Returns a list of taps as L < Tap|/type/Tap > objects.
152
+
119
153
= head2 method live
120
154
121
155
method live(Supply:D:) returns Bool:D
122
156
123
157
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
125
162
126
163
= head2 method wait
127
164
@@ -130,6 +167,20 @@ as soon as they arrive. Always returns C<True> in the default C<Supply>.
130
167
Waits until the supply is done (in which case it returns C < True > ) or C < quit >
131
168
(in which case it will throw the exception that was passed to C < quit > ).
132
169
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
+
133
184
= head2 method list
134
185
135
186
method list(Supply:D:) returns List:D
0 commit comments