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

[@xstate/graph] Add toGraph() #1366

Merged
merged 10 commits into from
Sep 10, 2020
Merged

[@xstate/graph] Add toGraph() #1366

merged 10 commits into from
Sep 10, 2020

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Aug 8, 2020

This PR introduces a helper function that converts machines to a directed graph structure, making it easy to use with graph-related algorithms, such as GraphViz and ELK.

import { toDirectedGraph } from '@xstate/graph';

const machine = createMachine({/* ... */});

const digraph = toDirectedGraph(machine);

// returns an object with this structure:
{
  id: '...',
  stateNode: /* StateNode */,
  children: [
    { id: '...', children: [/* ... */], edges: [/* ... */] },
    { id: '...', /* ... */ },
    // ...
  ],
  edges: [
    { source: /* ... */, target: /* ... */, transition: /* ... */ }
    // ...
  ]
}

See the snapshot for more details.

@changeset-bot
Copy link

changeset-bot bot commented Aug 8, 2020

🦋 Changeset is good to go

Latest commit: 142f54e

We got this.

This PR includes changesets to release 1 package
Name Type
@xstate/graph 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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 8, 2020

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 142f54e:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

} from './types';
import { getChildren } from 'xstate/lib/stateUtils';
Copy link
Member

Choose a reason for hiding this comment

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

What's the exact purpose of @xstate/graph? Seems like such an algorithm (getChildren) would, maybe, be a good candidate to be included here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call - the purpose of @xstate/graph is for graph-related utilities and helper functions, such as traversal, etc.

A good example of this is Cytoscape.

Copy link
Member

@Andarist Andarist left a comment

Choose a reason for hiding this comment

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

What not to like about this one? 😉 Great job!

Just please add an appropriate changeset so this will get added to the next release.

@davidkpiano davidkpiano merged commit 8392099 into master Sep 10, 2020
@davidkpiano davidkpiano deleted the davidkpiano/graph-tograph branch September 10, 2020 00:39
@github-actions github-actions bot mentioned this pull request Sep 10, 2020
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

2 participants