-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add guard inspection via _guards property on microstep events #5444
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 1d978fe The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
f320191 to
0bfbe8f
Compare
Add a new `@xstate.guard` inspection event type that is emitted when guards are evaluated during state transitions. This provides visibility into the guard evaluation process, making it easier to debug transition decisions. The event includes: - `guard.type`: The guard name (or '<inline>' for inline guards) - `guard.params`: The parameters passed to the guard - `result`: Whether the guard passed (true) or failed (false) This is useful for debugging questions like "why did the machine not transition to state X?" by showing which guards passed or failed. Example output: [machine-id] guard: canSubmit → false [machine-id] guard: hasValidInput → true Implementation details: - Added `InspectedGuardEvent` type to inspection.ts - Modified `evaluateGuard` to accept optional `actorScope` parameter - Threaded `actorScope` through the transition resolution call chain: - macrostep → selectTransitions → getTransitionData → transitionNode → StateNode.next → evaluateGuard - Guard events are only emitted when actorScope is provided (maintaining backwards compatibility) Co-Authored-By: Claude <noreply@anthropic.com>
0bfbe8f to
55b808c
Compare
| type: '@xstate.microstep'; | ||
| event: AnyEventObject; // { type: string, ... } | ||
| snapshot: Snapshot<unknown>; | ||
| _transitions: AnyTransitionDefinition[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new event, I would strongly recommend either enhancing _transitions with guard params (since if the transition is in this array, it was taken) and/or by adding _guards here instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha, i think the problem with _transitions is that it only shows the passed transitions - im partial to the _guards
Instead of emitting separate @xstate.guard events, guard evaluations are now collected and included as a _guards array on @xstate.microstep events. This keeps guard information correlated with their transitions. Co-Authored-By: Claude <noreply@anthropic.com>
Summary
This PR adds guard evaluation visibility to
@xstate.microstepinspection events via a new_guardsproperty. This provides insight into the guard evaluation process, making it easier to debug transition decisions.Motivation
When debugging state machines, it's often difficult to understand why a transition didn't happen. The existing inspection events (
@xstate.event,@xstate.action,@xstate.snapshot,@xstate.microstep) don't provide insight into guard evaluation - only the@xstate.microstepshows transitions that passed, but doesn't show which guards failed.This is particularly problematic when you have multiple guarded transitions for the same event and want to understand why a specific path wasn't taken. For example:
Without guard inspection, you can't easily tell which guards passed or failed.
Solution
Add a
_guardsproperty to@xstate.microstepinspection events that contains an array of all guard evaluations:This approach keeps guard information correlated with their transitions rather than emitting separate events.
Implementation
_guardsarray type toInspectedMicrostepEventininspection.tsevaluateGuard()inguards.tsto return guard metadata alongside the resultstateUtils.tsBreaking Changes
None - the
_guardsproperty is additive and existing code continues to work without modification.Test Plan
🤖 Generated with Claude Code