28555a9e7e9f8bf789fb0067a86fda795d6cf058
[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 1;