Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

onDone not working when the child state is final. #401

@veeramarni

Description

@veeramarni

Description

This issue happens in xstate-viz only but in the vscode xstate plugin works fine. When the child state is final, the parent's onDone need to be enabled and when onDone even is called it should go back to idle state but it just hangs in the visualizer without doing anything.

example

import { createMachine, assign } from 'xstate';

interface IngetrationContext {
    step: number;
    submittedData: any[];
}
export const test =
    createMachine(
        {
            context: {
                step: 0,
                submittedData: [],
            },
            initial: 'idle',

            states: {
                idle: {
                    always: [
                        { target: 'step1', cond: 'didStep1' },
                        { target: 'step2', cond: 'didStep2' },
                    ],

                    on: {
                        NEXT: [
                            {
                                target: 'idle',
                                cond: (ctx, event) => event.data !== '',
                                actions: 'incrementStep',
                            },
                            {
                                target: 'error',
                            },
                        ],

                        // ERROR: "error",
                        SUBMIT: [
                            {
                                target: 'loading',
                                cond: (ctx, event) => event.data !== '',
                            },
                            {
                                target: 'error',
                            },
                        ],

                        PREVIOUS: {
                            target: "idle",
                            actions: 'decrementStep',
                            internal: true
                        }
                    },
                },

                step1: {
                    onDone: {
                        target: 'idle',
                    },
                    states: {
                        'new state 1': {
                            invoke: {
                                src: 'startStep',
                                onDone: 'done',
                            },
                        },

                        done: {
                            type: 'final'
                        },
                    },

                    initial: 'new state 1',
                },

                step2: {
                    on: {
                        'Event 1': 'idle',
                    },
                },

                error: {
                    on: {
                        ERROR_HANDLED: 'idle',
                        SUBMIT: {
                            target: 'loading',
                            cond: (ctx, event) => event.data !== '',
                        },
                    },
                },

                loading: {
                    invoke: {
                        id: 'submitStepper',
                        //   src: () => submitForm(),
                        src: 'submitForm',
                        onDone: {
                            target: 'done',
                            actions: assign({ submittedData: (ctx, event) => event.data }),
                        },
                        onError: {
                            target: 'error',
                        },
                    },
                },

                done: {
                    type: 'final',
                }
            },
        },
        {
            actions: {
                incrementStep: assign((context, event) => {
                    console.log('-NEXT INCREMEN', context.step)
                    return {
                        step: context.step + 1,
                    }
                }),
                decrementStep: assign((context, event) => {
                    return {
                        step: context.step - 1,
                    }
                })
            },
            guards: {
                didStep1: (context, event) => {
                    return context.step === 1;
                },
                didStep2: (context, event) => {
                    return context.step === 2;
                },
            },
        },
    );

Expected Result

When onDone is clicked it should transition to next state.

Actual Result

It gets stuck at onDone

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions