Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Nested tree machines final state #100

Closed
kybarg opened this issue Mar 18, 2020 · 7 comments · Fixed by #104
Closed

[bug] Nested tree machines final state #100

kybarg opened this issue Mar 18, 2020 · 7 comments · Fixed by #104
Labels
bug Something isn't working

Comments

@kybarg
Copy link
Contributor

kybarg commented Mar 18, 2020

When there is a tree of nested state machines, when the very child final state is reached it goes straight to the root machine

@HipsterBrown
Copy link
Contributor

Can you share some example code or a link to codesandbox / codepen?

@kybarg
Copy link
Contributor Author

kybarg commented Mar 18, 2020

@HipsterBrown https://jsfiddle.net/kybarg/euqc1nyz/

@kybarg
Copy link
Contributor Author

kybarg commented Mar 18, 2020

@HipsterBrown output is

childStart
grandChildStart
grandChildDone
rootStart
rootStart

when expected output is

childStart
grandChildStart
grandChildDone
childResult

@kybarg
Copy link
Contributor Author

kybarg commented Mar 18, 2020

@matthewp any thoughts?

@HipsterBrown
Copy link
Contributor

It appears the invoke function type sends the done event to the interpreted service in general, rather than a specific machine, i.e the root machine vs the child machine, so the root done transition is happening when the grandChild machine has completed and invoked done.

let invokeType = {
  enter(machine, service, event) {
    this.fn.call(service, service.context, event)
      .then(data => service.send({ type: 'done', data }))
      .catch(error => service.send({ type: 'error', error }));
    return machine;
  }
};

My guess is this could be addressed by updating the invokeType to send that done event to a specific machine?

@matthewp
Copy link
Owner

Yes, this does sound like a bug. I can't look at it today but will try to do shortly (if someone else wants to try and do a PR that would be great too).

@matthewp matthewp added the bug Something isn't working label Mar 18, 2020
@kybarg
Copy link
Contributor Author

kybarg commented Mar 18, 2020

@matthewp
This seems like a solution, but it breaks test, so not sure...
kybarg@d12adc2#diff-34d800fceb92f13afae91cf2013e361fR93-R106

const machineToThen = machine => function(ctx, ev) {
  return {
    then: resolve => {
      this.child = interpret(machine, s => {
-        this.onChange(this);
+        if (this.child !== s) this.onChange(s);
+        else this.onChange(this);
        if(s.machine.state.value.final) {
          delete this.child;
          resolve(s.context);
        }
      }, ctx, ev);
      return { catch: identity };
    }
  };
};

@kybarg kybarg changed the title Nested tree machines final state [bug] Nested tree machines final state Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants