move backtracking into a 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->backtrack;
17     } else {
18       $self->for_state
19     }
20   };
21   my $state = $self->_current_state($start_state->run);
22   return +{
23     map +($_ => $state->scope_var($_)->bound_value), keys %{$state->scope}
24   };
25 }
26
27 1;