Skip to content

Commit

Permalink
Merge pull request #24 from statelyai/davidkpiano/better-serialization
Browse files Browse the repository at this point in the history
Better serialization
  • Loading branch information
davidkpiano authored Apr 22, 2024
2 parents 537a8ad + 9cfc0b3 commit 5c36702
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-tomatoes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@statelyai/inspect': minor
---

The `serialize` option will now pre-serialize the event using `superjson` before the custom serialization.
21 changes: 18 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import safeStringify from 'fast-safe-stringify';
import { AnyEventObject, Observer, Subscribable, toObserver } from 'xstate';
import { InspectorOptions, createInspector } from './createInspector';
import {
InspectorOptions,
createInspector,
defaultInspectorOptions,
} from './createInspector';
import { Adapter, Inspector, StatelyInspectionEvent } from './types';
import { UselessAdapter } from './useless';

Expand Down Expand Up @@ -165,7 +169,14 @@ export class BrowserAdapter implements Adapter {
) {
this.status = 'connected';
this.deferredEvents.forEach((event) => {
const serializedEvent = this.options.serialize(event);
const preSerializedEvent = defaultInspectorOptions.serialize(
event,
event
);
const serializedEvent = this.options.serialize(
preSerializedEvent,
event
);
this.targetWindow?.postMessage(serializedEvent, '*');
});
}
Expand All @@ -184,7 +195,11 @@ export class BrowserAdapter implements Adapter {
if (this.options.send) {
this.options.send(event);
} else if (this.status === 'connected') {
const serializedEvent = this.options.serialize(event);
const preSerializedEvent = defaultInspectorOptions.serialize(
event,
event
);
const serializedEvent = this.options.serialize(preSerializedEvent, event);
this.targetWindow?.postMessage(serializedEvent, '*');
}

Expand Down
2 changes: 1 addition & 1 deletion src/createInspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test('Creates an inspector for a state machine', async () => {
},
{
"event": {
"input": undefined,
"input": null,
"type": "xstate.init",
},
"sessionId": "x:0",
Expand Down
16 changes: 11 additions & 5 deletions src/createInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AnyActorRef, InspectionEvent, Snapshot } from 'xstate';
import pkg from '../package.json';
import { idleCallback } from './idleCallback';
import safeStringify from 'safe-stable-stringify';
import { serialize } from 'superjson';

function getRoot(actorRef: AnyActorRef) {
let marker: AnyActorRef | undefined = actorRef;
Expand All @@ -33,7 +34,10 @@ function getRootId(actorRefOrId: AnyActorRef | string): string | undefined {

export interface InspectorOptions {
filter?: (event: StatelyInspectionEvent) => boolean;
serialize?: (event: StatelyInspectionEvent) => StatelyInspectionEvent;
serialize?: (
event: StatelyInspectionEvent,
originalEvent: StatelyInspectionEvent
) => StatelyInspectionEvent;
/**
* Whether to automatically start the inspector.
*
Expand All @@ -44,7 +48,8 @@ export interface InspectorOptions {

export const defaultInspectorOptions: Required<InspectorOptions> = {
filter: () => true,
serialize: (event) => event,
serialize: (event) =>
serialize(event).json as unknown as StatelyInspectionEvent,
autoStart: true,
};

Expand All @@ -57,10 +62,11 @@ export function createInspector<TAdapter extends Adapter>(
// Event filtered out
return;
}
const serializedEvent = options?.serialize?.(event) ?? event;
// idleCallback(() => {
const preSerializedEvent = defaultInspectorOptions.serialize(event, event);
const serializedEvent =
options?.serialize?.(preSerializedEvent, event) ?? preSerializedEvent;

adapter.send(serializedEvent);
// })
}
const inspector: Inspector<TAdapter> = {
adapter,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export type {
StatelySnapshotEvent,
} from './types';
export { createWebSocketInspector, createWebSocketReceiver } from './webSocket';
export { createReduxDevToolsInspector } from './redux';

0 comments on commit 5c36702

Please sign in to comment.