Skip to content

Commit

Permalink
Fix test breakage caused by 48cc6b5
Browse files Browse the repository at this point in the history
Implements:
- Channel.receive-nil-on-close
- Channel.iterator
- Channel.Seq
Re-Implements:
- Channel.list

Also: RT #127968 was a dup of RT #127960
  • Loading branch information
lizmat committed Apr 23, 2016
1 parent 48cc6b5 commit 5a14162
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/core/Channel.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,28 @@ my class Channel {
Nil
}

method receive(Channel:D:) {
method !receive(Channel:D: $fail-on-close) {
my \msg := nqp::shift($!queue);
if nqp::istype(msg, CHANNEL_CLOSE) {
nqp::push($!queue, msg); # make sure other readers see it
$!closed_promise_vow.keep(Nil);
X::Channel::ReceiveOnClosed.new(channel => self).throw
if $fail-on-close;
Nil
}
elsif nqp::istype(msg, CHANNEL_FAIL) {
nqp::push($!queue, msg); # make sure other readers see it
$!closed_promise_vow.break(msg.error);
die msg.error;
}
msg
else {
msg
}
}

method receive(Channel:D:) { self!receive(1) }
method receive-nil-on-close(Channel:D:) { self!receive(0) }

method poll(Channel:D:) {
my \msg := nqp::queuepoll($!queue);
if nqp::isnull(msg) {
Expand Down Expand Up @@ -128,18 +135,21 @@ my class Channel {
}
}

multi method list(Channel:D:) {
Seq.new( class :: does Iterator {
method iterator(Channel:D:) {
class :: does Iterator {
has $!channel;
method !SET-SELF($!channel) { self }
method new(\c) { nqp::create(self)!SET-SELF(c) }
method pull-one() {
my Mu \got = $!channel.poll;
my Mu \got = $!channel.receive-nil-on-close;
nqp::eqaddr(got, Nil) ?? IterationEnd !! got
}
}.new(self))
}.new(self)
}

method Seq(Channel:D:) { Seq.new(self.iterator) }
method list(Channel:D:) { self.Seq.list }

method close() {
$!closed = 1;
nqp::push($!queue, CHANNEL_CLOSE);
Expand Down

0 comments on commit 5a14162

Please sign in to comment.