xstate icon indicating copy to clipboard operation
xstate copied to clipboard

Improve example templates

Open davidkpiano opened this issue 3 years ago • 3 comments

This PR improves the templates to use a new timeMachine that fetches the current time and assigns it to context:

const timeMachine = createMachine(
  {
    id: 'time',
    initial: 'idle',
    context: {
      time: undefined
    },
    states: {
      idle: {
        on: {
          fetch: 'pending'
        }
      },
      pending: {
        invoke: {
          src: 'fetchTime',
          onDone: {
            actions: assign({
              time: (_, event) => event.data
            })
          }
        },
        on: {
          cancel: 'idle'
        }
      },
      success: {
        on: {
          refetch: 'pending'
        }
      }
    }
  },
  {
    services: {
      fetchTime: () =>
        new Promise((res) => {
          setTimeout(() => {
            res(new Date().toLocaleTimeString());
          }, 1000);
        })
    }
  }
);

The goal of this new template is to provide a statechart example that isn't as contrived as the original toggle example, has more states and transitions, and also shows the usefulness of invoking states.

davidkpiano avatar Apr 29 '22 19:04 davidkpiano

⚠️ No Changeset found

Latest commit: 2ecf59494c8f08d1992961aa8fbcbc97eeaa1f77

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Apr 29 '22 19:04 changeset-bot[bot]

👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

ghost avatar Apr 29 '22 19:04 ghost

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2ecf59494c8f08d1992961aa8fbcbc97eeaa1f77:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

codesandbox-ci[bot] avatar Apr 29 '22 19:04 codesandbox-ci[bot]