@@ -7,7 +7,13 @@ import {
7
7
} from './types' ;
8
8
import { toEventObject } from './utils' ;
9
9
import { Inspector } from './types' ;
10
- import { AnyActorRef , InspectionEvent , Snapshot } from 'xstate' ;
10
+ import {
11
+ AnyActorRef ,
12
+ AnyEventObject ,
13
+ InspectionEvent ,
14
+ MachineContext ,
15
+ Snapshot ,
16
+ } from 'xstate' ;
11
17
import pkg from '../package.json' ;
12
18
import { idleCallback } from './idleCallback' ;
13
19
import safeStringify from 'safe-stable-stringify' ;
@@ -40,29 +46,74 @@ export interface InspectorOptions {
40
46
* @default true
41
47
*/
42
48
autoStart ?: boolean ;
49
+ /**
50
+ * The maximum number of deferred events to hold in memory until the inspector is active.
51
+ * If the number of deferred events exceeds this number, the oldest events will be dropped.
52
+ *
53
+ * @default 200
54
+ */
43
55
maxDeferredEvents ?: number ;
56
+
57
+ /**
58
+ * Sanitizes events sent to actors. Only the sanitized event will be sent to the inspector.
59
+ */
60
+ sanitizeEvent ?: ( event : AnyEventObject ) => AnyEventObject ;
61
+
62
+ /**
63
+ * Sanitizes actor snapshot context. Only the sanitized context will be sent to the inspector.
64
+ */
65
+ sanitizeContext ?: ( context : MachineContext ) => MachineContext ;
44
66
}
45
67
46
68
export const defaultInspectorOptions : Required < InspectorOptions > = {
47
69
filter : ( ) => true ,
48
70
serialize : ( event ) => event ,
49
71
autoStart : true ,
50
72
maxDeferredEvents : 200 ,
73
+ sanitizeEvent : ( event ) => event ,
74
+ sanitizeContext : ( context ) => context ,
51
75
} ;
52
76
53
77
export function createInspector < TAdapter extends Adapter > (
54
78
adapter : TAdapter ,
55
79
options ?: InspectorOptions
56
80
) : Inspector < TAdapter > {
57
- function sendAdapter ( event : StatelyInspectionEvent ) : void {
58
- if ( options ?. filter && ! options . filter ( event ) ) {
81
+ function sendAdapter ( inspectionEvent : StatelyInspectionEvent ) : void {
82
+ if ( options ?. filter && ! options . filter ( inspectionEvent ) ) {
59
83
// Event filtered out
60
84
return ;
61
85
}
62
- const serializedEvent = options ?. serialize ?.( event ) ?? event ;
63
- // idleCallback(() => {
86
+
87
+ const sanitizedEvent : typeof inspectionEvent =
88
+ options ?. sanitizeContext || options ?. sanitizeEvent
89
+ ? inspectionEvent
90
+ : {
91
+ ...inspectionEvent ,
92
+ } ;
93
+ if (
94
+ options ?. sanitizeContext &&
95
+ ( sanitizedEvent . type === '@xstate.actor' ||
96
+ sanitizedEvent . type === '@xstate.snapshot' )
97
+ ) {
98
+ sanitizedEvent . snapshot = {
99
+ ...sanitizedEvent . snapshot ,
100
+ // @ts -ignore
101
+ context : options . sanitizeContext (
102
+ // @ts -ignore
103
+ sanitizedEvent . snapshot . context
104
+ ) ,
105
+ } ;
106
+ }
107
+ if (
108
+ options ?. sanitizeEvent &&
109
+ ( sanitizedEvent . type === '@xstate.event' ||
110
+ sanitizedEvent . type === '@xstate.snapshot' )
111
+ ) {
112
+ sanitizedEvent . event = options . sanitizeEvent ( sanitizedEvent . event ) ;
113
+ }
114
+ const serializedEvent =
115
+ options ?. serialize ?.( sanitizedEvent ) ?? sanitizedEvent ;
64
116
adapter . send ( serializedEvent ) ;
65
- // })
66
117
}
67
118
const inspector : Inspector < TAdapter > = {
68
119
adapter,
0 commit comments