Xstate: Should after trigger `entry` actions?

Created on 24 Sep 2019  路  8Comments  路  Source: davidkpiano/xstate

See snippet below, if target is not changed, it will trigger entry action again, hence causing it to loop endlessly, wondering is this by design?

states: {
 loading: {
  entry: 'reloadItems',

  after: {
   // 2000: 'master' // ok
   // 2000: { target: 'master'}, // ok
   2000: {
    // target: 'master', // 猬咃笍if target didn't change, will trigger 'entry' to run again, hence causing finite loops
    actions: ctx => console.log( '\n\ntimer up',  )
   }
  },
 }
}
bug question

All 8 comments

Thanks for the report - I'm going to dig into this in following days to assess what's the proper behavior and fix it if needed.

The default "transition type" for transitions is "external": https://www.w3.org/TR/scxml/#transition

Treat this like a normal transition. Do you still get the same issue if you have this?

2000: {
  internal: true,
  actions: ctx => console.log(...)
}

Adding internal: true fixed it, thanks for the heads-up!

Just to recap:

{
  foo: {
    after: {
      2000: {
        internal: false,
        actions: ctx => console.log('timer up')
      }
    }
  }
}

executes that action once - regardless of the internal setting of the transition.

The OP's original problem was about this situation

{
  foo: {
    after: {
      2000: {
        target: 'foo',
        internal: false,
        actions: ctx => console.log('timer up')
      }
    }
  }
}

where internal setting matters - for internal: false (default) the action gets repeatedly executed because the foo state is being reentered all over again, but for internal: true it gets also fired once because we don't have a reentry situation then (due to nature of internal transitions).

Just to be clear, following snippet caused problem, the key is not assigning a different target, hence it's entering to the same state again and again, setting internal:true have it fixed.

after: {
    2000: {
        internal: false,
        actions: ctx => console.log( 'timer up',  )
    }
},

This got me confused initially - because it seemed like you describe the situation like this, but I couldn't repro it - https://codesandbox.io/s/jolly-frog-u1o2e .

That's why I've added a summary of the things discussed in here which I have hoped might cause you to point some blanks in it.

Because I can't repro it - could you prepare a simple repro case? I could then investigate why it's acting different for you and me (which seems like a possible bug).

@Andarist Here's a minimum sample to have it reproduced, have fun 馃槄

Thank you for getting back with a repro! It has allowed me to diagnose & fix the issue. Only targetless delayed transitions were affected and it was not happening on 4.6.x (which I have used for my repro attempts)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

laurentpierson picture laurentpierson  路  3Comments

amelon picture amelon  路  3Comments

3plusalpha picture 3plusalpha  路  3Comments

dakom picture dakom  路  3Comments

hnordt picture hnordt  路  3Comments