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

[core] Tagged states #2143

Merged
merged 10 commits into from
Apr 29, 2021
Merged

[core] Tagged states #2143

merged 10 commits into from
Apr 29, 2021

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Apr 24, 2021

This PR introduces the tags property for state nodes. The tags for states in the state configuration are merged into a state.tags: Set<string> property:

const machine = createMachine({
  initial: 'green',
  states: {
    green: {
      tags: ['go']
    },
    yellow: {
      tags: ['go'],
      on: {
        TIMER: 'red'
      }
    },
    red: {
      tags: ['stop']
    }
  }
});

machine.initialState.tags.has('go'); // true

cc. @mogsie

@changeset-bot
Copy link

changeset-bot bot commented Apr 24, 2021

🦋 Changeset detected

Latest commit: a3cdf96

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
xstate Minor

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Contributor

@with-heart with-heart left a comment

Choose a reason for hiding this comment

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

This is so cool! Such a small change to add such potentially useful functionality.

Copy link
Collaborator

@mogsie mogsie left a comment

Choose a reason for hiding this comment

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

I approve of this. 😁

packages/core/src/State.ts Show resolved Hide resolved
packages/core/src/StateNode.ts Outdated Show resolved Hide resolved
packages/core/src/StateNode.ts Outdated Show resolved Hide resolved
packages/core/test/tags.test.ts Show resolved Hide resolved
packages/core/test/tags.test.ts Outdated Show resolved Hide resolved
});
```

You can query whether a state has a tag via `state.hasTag(tag)`:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also mention state.tags or is that not something you want advertised?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not right now, I think that'll be an implementation detail for now.

@davidkpiano davidkpiano merged commit a00ca34 into master Apr 29, 2021
@github-actions github-actions bot mentioned this pull request Apr 29, 2021
@Andarist Andarist deleted the davidkpiano/tags branch March 22, 2023 15:03
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

6 participants