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

Improve example templates #3261

Closed
wants to merge 5 commits into from
Closed

Improve example templates #3261

wants to merge 5 commits into from

Conversation

davidkpiano
Copy link
Member

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.

@changeset-bot
Copy link

changeset-bot bot commented Apr 29, 2022

⚠️ No Changeset found

Latest commit: 2ecf594

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

@ghost
Copy link

ghost commented Apr 29, 2022

👇 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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 29, 2022

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 2ecf594:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

Comment on lines +16 to +20
idle: {
on: {
fetch: 'pending'
}
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this example. One thing for clarity might be worth capitalizing states and actions or using some type of differentiation to say that those are user-defined and not special xstate keys. especially when things like fetch already exist in javascript.

"build": "parcel build index.html"
},
"dependencies": {
"parcel-bundler": "1.12.5",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this usually should be a dev dep

},
"dependencies": {
"parcel-bundler": "1.12.5",
"xstate": "^4.32.1"
},
"devDependencies": {
"typescript": "^4.7.4",
"vite": "^2.9.13"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have switched to using parcel for this example so vite should be dropped from here

@@ -503,10 +503,10 @@

model.createMachine({
// `ctx` was of type `any`
entry: ctx => {},
entry: (ctx) => {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using a different Prettier version than Changesets and this results in a lot of those reformatting changes. I have a PR open that is supposed to fix this: changesets/changesets#905

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

Successfully merging this pull request may close these issues.

None yet

3 participants