trexitycode / wirestate

MIT License
1 stars 0 forks source link

Equivalent of `event: { target: undefined }` #3

Closed matb33 closed 5 years ago

matb33 commented 5 years ago

We should explore a way to re-create event: undefined. Here's a potential example:

@machine App
  Start
    go -> Things
  Things
    back -> Start
    @use Things

@machine Things
  Loading
    done -> Showing
    error -> Error
    back ->|
  Showing
  Error
    try again -> Loading

While "loading" things, we don't want back to bubble up. But with the other states, it's fair game.

dschnare commented 5 years ago

Could we do:

@machine Things
  Loading
    done -> Showing
    error -> Error
    back -> Loading

Is that the same thing? Or would XState exit and re-enter the Loading state?

matb33 commented 5 years ago

Yep, XState will exit and re-enter. It's also a fairly known pattern now too. If you look at the xstate visualizer default example, they do it there (https://statecharts.github.io/xstate-viz/)

...
        failure: {
          on: {
            RETRY: undefined,
          },
          type: 'final'
        }
...
dschnare commented 5 years ago

Note for myself: When implementing support for this, add | as a symbol in the tokenizer. Then in the parser handle the -> symbol followed by the | symbol appropriately. When compiling to XState the transition should be compiled to:

{
  ...
  "on": {
    "my event": {
      "actions": []
    }
  }
  ...
}

Reason being is that the compiler leverages JSON.stringify and if we simply set the transition target to undefined then no property will be written to the compiled XState statechart since when stringifying undefined props are stripped.

See: https://xstate.js.org/docs/guides/transitions.html#forbidden-transitions