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

Actions or guards are not recognized when inside of delayed transitions #8

Closed
BrunoQuaresma opened this issue Sep 10, 2021 · 3 comments

Comments

@BrunoQuaresma
Copy link

The following code shows some errors

import { assign, createMachine } from "xstate";

type CardValue = string;
type CardIndex = number;

type GameContext = {
  flippedCards: Array<CardIndex>;
  board: Array<CardValue | undefined>;
  rows: number;
  columns: number;
};

type FlipCardEvent = {
  type: "FLIP_CARD";
  index: CardIndex;
};

type GameEvent = FlipCardEvent;

const flipCard = assign<GameContext, GameEvent>((context, event) => ({
  flippedCards: context.flippedCards.concat(event.index),
}));

const untapCards = assign<GameContext, GameEvent>(() => ({
  flippedCards: [],
}));

const removeFlippedCards = assign<GameContext, GameEvent>((context) => {
  const [first, second] = context.flippedCards;
  let updatedBoard = context.board;
  updatedBoard[first] = undefined;
  updatedBoard[second] = undefined;

  return {
    flippedCards: [],
    board: updatedBoard,
  };
});

const flippedCardsAreMatching = (context: GameContext) => {
  const [first, second] = context.flippedCards;
  return context.board[first] === context.board[second];
};

const hasNoAvailableCards = (context: GameContext) => {
  return context.board.every((card) => card === undefined);
};

export const gameMachine = createMachine<GameContext, GameEvent>(
  {
    id: "Game",
    initial: "idle",
    context: {
      flippedCards: [],
      board: ["a", "b", "a", "b"],
      columns: 2,
      rows: 2,
    },
    states: {
      idle: {
        on: {
          FLIP_CARD: {
            target: "flipedFirstCard",
            actions: "flipCard",
          },
        },
      },
      flipedFirstCard: {
        on: {
          FLIP_CARD: {
            target: "checkResult",
            actions: "flipCard",
          },
        },
      },
      checkResult: {
        after: {
          1000: [
            {
              target: "success",
              cond: "flippedCardsAreMatching",
              actions: "removeFlippedCards",
            },
            { target: "fail", actions: "untapCards" },
          ],
        },
      },
      success: {
        always: [
          { target: "end", cond: "hasNoAvailableCards" },
          { target: "idle" },
        ],
      },
      fail: {
        always: "idle",
      },
      end: {
        type: "final",
      },
    },
  },
  {
    actions: {
      flipCard,
      removeFlippedCards,
      untapCards,
    },
    guards: {
      flippedCardsAreMatching,
      hasNoAvailableCards,
    },
  }
);

Screen Shot 2021-09-10 at 20 45 25

@mattpocock
Copy link
Contributor

@BrunoQuaresma Thanks for the repro! Will look at this on Monday

mattpocock added a commit to statelyai/xstate-parser that referenced this issue Sep 12, 2021
@mattpocock
Copy link
Contributor

Fixed in 1.1.9

@BrunoQuaresma
Copy link
Author

So fast, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants