rewrite file correctly chaining from create file
[scpubgit/DKit.git] / lib / DX / State.pm
index 2f0e2b0..b75a39d 100644 (file)
@@ -3,6 +3,7 @@ package DX::State;
 use Return::MultiLevel qw(with_return);
 use DX::Op::Backtrack;
 use Scalar::Util qw(blessed);
+use List::MoreUtils qw(uniq);
 use Safe::Isa;
 use Moo;
 
@@ -24,6 +25,8 @@ has facts => (is => 'ro');
 
 has dependencies => (is => 'ro', default => sub { {} });
 
+has actions => (is => 'ro', default => sub { {} });
+
 sub scope_var {
   my ($self, $name) = @_;
   $self->by_id->{$self->scope->{$name}};
@@ -31,6 +34,8 @@ sub scope_var {
 
 sub resolve_value {
   my ($self, $var) = @_;
+die("FUCK") unless $var;
+  die "Can't resolve unbound ${\$var->id}" unless $var->is_bound;
   my $val = $var->bound_value;
   if ($val->$_does('DX::Role::Ref')) {
     return $val->resolve($self);
@@ -61,6 +66,18 @@ sub expand_vars {
   ), %vars;
 }
 
+sub record_action {
+  my ($self, $action) = @_;
+  my %id_gen = %{$self->id_gen};
+  my ($type) = (ref($action) =~ /([^:]+)$/);
+  my $id = join('_', $type, ++($id_gen{$type}||='000'));
+  my $recorded = $action->but(id => $id);
+  $self->but(
+    id_gen => \%id_gen,
+    actions => { %{$self->actions}, $id => $recorded }
+  ), $id;
+}
+
 sub assign_vars {
   my ($self, %vars) = @_;
   my ($state, %expanded) = $self->expand_vars(%vars);
@@ -188,11 +205,11 @@ sub action_dependencies {
   while (my $id = shift @queue) {
     $seen{$id}++;
     my $value = $self->resolve_value($by_id->{$id});
-    push @found, $id if $value->$_does('DX::Role::Fact')
-                        and $value->has_required_action;
+    push @found, $value if $value->$_does('DX::Role::Fact')
+                           and $value->has_required_action;
     push @queue, grep !$seen{$_}, keys %{$deps->{$id}};
   }
-  return @found;
+  return uniq map $_->required_action, @found;
 }
 
 sub copy_vars {