Xstate: Upgrade to v3.3.1 causes error

Created on 8 Jun 2018  Â·  5Comments  Â·  Source: davidkpiano/xstate

Bug or feature request?

Bug

Description:

I've been messing around with xstate and working out how to build parallel and hierarchical state machines. I've made a contrived state machine as a sort of working example for a project I'm working on. I then run through the states in the machine, using setTimeout and console.log-ing as the state changes.

This worked fine with the previous version of xstate that I was using - v.3.2.1 . Yesterday I updated to v3.3.1, and now running my machine I get the following error:

(Bug) Expected result:

Not to get any errors (or only ones that can be traced to my code).

(Bug) Actual result:

/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:103
    var result = exports.flatMap(Object.keys(stateValue).map(function (key) {
                                        ^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.exports.toStatePaths (/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:103:41)
    at /home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:104:24
    at Array.map (<anonymous>)
    at Object.exports.toStatePaths (/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:103:58)
    at /home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:104:24
    at Array.map (<anonymous>)
    at Object.exports.toStatePaths (/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/utils.js:103:58)
    at StateNode._transitionOrthogonalNode (/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/StateNode.js:234:28)
    at StateNode._transition (/home/jo/dev/joState/stateMachine/node_modules/xstate/lib/StateNode.js:248:21)

(Bug) Potential fix:

I've tried to debug my own code to see if there's a problem there, but I really can't find anything that would cause an error. I'm aware of some flaws in how I'm implementing my machine, but those are design flaws rather than breaking code.

Really enjoying the library by the way, and the visualiser is a great addition. Thanks for working on this :)

Link to reproduction or proof-of-concept:

Download and run my project here

bug

Most helpful comment

Awesome - 3.3.2 fixes my issue. And @mogsie your debugger is very useful.
Thank you both!

All 5 comments

Additionally, I went into the xstate directory in node_modules, so that I could run the unit tests and mess around trying to find bugs in my code.
On running npm i there, I get:

src/scxml.ts:195:47 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
  Type 'undefined' is not assignable to type 'string | number'.

195         transition.actions.push(actions.raise(element.attributes!.event));
                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~


src/scxml.ts:293:36 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
  Type 'undefined' is not assignable to type 'string | number'.

293               return actions.raise(element.attributes!.event);
                                       ~~~~~~~~~~~~~~~~~~~~~~~~~


src/scxml.ts:304:36 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
  Type 'undefined' is not assignable to type 'string | number'.

304               return actions.raise(element.attributes!.event);
                                       ~~~~~~~~~~~~~~~~~~~~~~~~~


src/scxml.ts:343:31 - error TS2339: Property 'elements' does not exist on type 'ElementCompact | Element'.
  Property 'elements' does not exist on type 'ElementCompact'.

343   const machineElement = json.elements.filter(
                                  ~~~~~~~~


Hi! I just took a quick look at the repo you linked and I'm reproducing it in my debugger. If you open the chrome debugger, you can catch the debugger when it throws the exception and start poking around.

The call stack is something like:

transition → transitionHierarchcialNode → Transition → TransitionOrthogonalNode → allPaths → anonymous → allPaths → anonymous → allPaths()

The transitionOrthogonalNode calls allPaths with the value

{
  dependenciesResolving: {
    magpie: "inProgress", 
    morrigu: undefined 
  }
}

The machine itself, before that transition is in the state

{"dependenciesResolving": {
    "magpie": "inProgress",
    "morrigu": {
      "dependenciesResolving": {
        "searchlens": "pending",
        "googleAnalytics": "pending"
      }
    }
  }
}

morrigu is the name of the region which wants to transition between states. Hope this helps @davidkpiano

You can ignore scxml.ts - it's not used in the core library.

Additionally, can you please reproduce your code to a minimal test case? It's a bit difficult to debug as-is.

Thanks; yes I'll try to simplify the machine and see if I can still reproduce the error.
I'd managed to get to morrigu: undefined (via a different route)... I'm just not sure _why_ it's undefined.
I'll keep digging.

Awesome - 3.3.2 fixes my issue. And @mogsie your debugger is very useful.
Thank you both!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hnordt picture hnordt  Â·  3Comments

greggman picture greggman  Â·  3Comments

rodinhart picture rodinhart  Â·  3Comments

laurentpierson picture laurentpierson  Â·  3Comments

suku-h picture suku-h  Â·  3Comments