-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection event for microsteps (#4693)
* Add @xstate/microstep inspection event + tests * Optimization: make _sendInspectionEvent "lazy-ish" * Revert StateMachine.ts * Improve types * Update packages/core/src/stateUtils.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/core/src/stateUtils.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Add tests * Reusable util * Add inspection.ts * Add @xstate.action inspection event * Update test * Make sendInspectionEvent non-optional * Changeset * Rename .transitions to ._transitions * Changeset * Update packages/core/src/stateUtils.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update packages/core/src/stateUtils.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Update * Update test snapshots --------- Co-authored-by: Mateusz Burzyński <[email protected]>
- Loading branch information
1 parent
7a8796f
commit 11b6a1a
Showing
16 changed files
with
838 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
You can now inspect microsteps (`@xstate.microstep`) and actions (`@xstate.action`): | ||
|
||
```ts | ||
const machine = createMachine({ | ||
initial: 'a', | ||
states: { | ||
a: { | ||
on: { | ||
event: 'b' | ||
} | ||
}, | ||
b: { | ||
entry: 'someAction', | ||
always: 'c' | ||
}, | ||
c: {} | ||
} | ||
}); | ||
|
||
const actor = createActor(machine, { | ||
inspect: (inspEvent) => { | ||
if (inspEvent.type === '@xstate.microstep') { | ||
console.log(inspEvent.snapshot); | ||
// logs: | ||
// { value: 'a', … } | ||
// { value: 'b', … } | ||
// { value: 'c', … } | ||
|
||
console.log(inspEvent.event); | ||
// logs: | ||
// { type: 'event', … } | ||
} else if (inspEvent.type === '@xstate.action') { | ||
console.log(inspEvent.action); | ||
// logs: | ||
// { type: 'someAction', … } | ||
} | ||
} | ||
}); | ||
|
||
actor.start(); | ||
|
||
actor.send({ type: 'event' }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
AnyActorRef, | ||
AnyEventObject, | ||
AnyTransitionDefinition, | ||
Snapshot | ||
} from './types.ts'; | ||
|
||
export type InspectionEvent = | ||
| InspectedSnapshotEvent | ||
| InspectedEventEvent | ||
| InspectedActorEvent | ||
| InspectedMicrostepEvent | ||
| InspectedActionEvent; | ||
|
||
export interface BaseInspectionEventProperties { | ||
rootId: string; // the session ID of the root | ||
/** | ||
* The relevant actorRef for the inspection event. | ||
* - For snapshot events, this is the `actorRef` of the snapshot. | ||
* - For event events, this is the target `actorRef` (recipient of event). | ||
* - For actor events, this is the `actorRef` of the registered actor. | ||
*/ | ||
actorRef: AnyActorRef; | ||
} | ||
|
||
export interface InspectedSnapshotEvent extends BaseInspectionEventProperties { | ||
type: '@xstate.snapshot'; | ||
event: AnyEventObject; // { type: string, ... } | ||
snapshot: Snapshot<unknown>; | ||
} | ||
|
||
export interface InspectedMicrostepEvent extends BaseInspectionEventProperties { | ||
type: '@xstate.microstep'; | ||
event: AnyEventObject; // { type: string, ... } | ||
snapshot: Snapshot<unknown>; | ||
_transitions: AnyTransitionDefinition[]; | ||
} | ||
|
||
export interface InspectedActionEvent extends BaseInspectionEventProperties { | ||
type: '@xstate.action'; | ||
action: { | ||
type: string; | ||
params: Record<string, unknown>; | ||
}; | ||
} | ||
|
||
export interface InspectedEventEvent extends BaseInspectionEventProperties { | ||
type: '@xstate.event'; | ||
// The source might not exist, e.g. when: | ||
// - root init events | ||
// - events sent from external (non-actor) sources | ||
sourceRef: AnyActorRef | undefined; | ||
event: AnyEventObject; // { type: string, ... } | ||
} | ||
|
||
export interface InspectedActorEvent extends BaseInspectionEventProperties { | ||
type: '@xstate.actor'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.