Skip to content

Commit

Permalink
Merge pull request #30 from statelyai/davidkpiano/maxDeferredEvents
Browse files Browse the repository at this point in the history
Add `maxDeferredEvents`
  • Loading branch information
davidkpiano authored Jul 6, 2024
2 parents 35c540b + 15e24d4 commit d3861d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion 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 @@ -54,6 +58,7 @@ export function createBrowserInspector(
}

const resolvedOptions = {
...defaultInspectorOptions,
url: 'https://stately.ai/inspect',
filter: () => true,
serialize: (inspectionEvent) => JSON.parse(safeStringify(inspectionEvent)),
Expand Down Expand Up @@ -189,5 +194,10 @@ export class BrowserAdapter implements Adapter {
}

this.deferredEvents.push(event);

// Remove the oldest event if we've reached the max deferred events
if (this.deferredEvents.length > this.options.maxDeferredEvents) {
this.deferredEvents.shift();
}
}
}
2 changes: 2 additions & 0 deletions src/createInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ export interface InspectorOptions {
* @default true
*/
autoStart?: boolean;
maxDeferredEvents?: number;
}

export const defaultInspectorOptions: Required<InspectorOptions> = {
filter: () => true,
serialize: (event) => event,
autoStart: true,
maxDeferredEvents: 200,
};

export function createInspector<TAdapter extends Adapter>(
Expand Down
5 changes: 5 additions & 0 deletions src/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class WebSocketAdapter implements Adapter {

constructor(options?: WebSocketInspectorOptions) {
this.options = {
...defaultInspectorOptions,
filter: () => true,
serialize: (inspectionEvent) =>
JSON.parse(safeStringify(inspectionEvent)),
Expand Down Expand Up @@ -74,6 +75,10 @@ export class WebSocketAdapter implements Adapter {
this.ws.send(safeStringify(inspectionEvent));
} else {
this.deferredEvents.push(inspectionEvent);

if (this.deferredEvents.length > this.options.maxDeferredEvents) {
this.deferredEvents.shift();
}
}
}
}
Expand Down

0 comments on commit d3861d4

Please sign in to comment.