test more complex stream
[scpubgit/DKit.git] / lib / DX / ResultStream.pm
1 package DX::ResultStream;
2
3 use Moo;
4
5 has for_state => (is => 'ro', required => 1);
6
7 has _current_state => (is => 'rw');
8
9 has is_exhausted => (is => 'rwp');
10
11 sub next {
12   my ($self) = @_;
13   return if $self->is_exhausted;
14   my $start_state = do {
15     if (my $cur = $self->_current_state) {
16       $cur->push_backtrack;
17     } else {
18       $self->for_state
19     }
20   };
21   my $state = $self->_current_state($start_state->run);
22   unless ($state) {
23     $self->_set_is_exhausted(1);
24     return;
25   }
26   return +{
27     map +($_ => $state->scope_var($_)->bound_value), keys %{$state->scope}
28   };
29 }
30
31 sub results {
32   my ($self) = @_;
33   my @all;
34   while (my $next = $self->next) {
35     push @all, $next;
36   }
37   return @all;
38 }
39
40 1;