npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xstate-inspect-no-fast-stringify

v0.0.1

Published

XState inspection utilities

Downloads

5

Readme

@xstate/inspect

Inspection tools for XState.

Inspector running from CodeSandbox

See CodeSandbox example here

Installation

  1. Install with npm or yarn:
npm install @xstate/inspect
# or yarn add @xstate/inspect
  1. Import it at the beginning of your project, before any other code is called:
import { inspect } from '@xstate/inspect';

inspect({
  // options
  // url: 'https://statecharts.io/inspect', // (default)
  iframe: false // open in new window
});
  1. Add { devTools: true } to any interpreted machines you want to visualize:
import { interpret } from 'xstate';
import { inspect } from '@xstate/inspect';
// ...

const service = interpret(someMachine, { devTools: true });

Inspect Options

// defaults
inspect({
  iframe: () => document.querySelector('iframe[data-xstate]'),
  url: 'https://statecharts.io/inspect'
});

// the above is the same as this:
inspect();

Arguments: the options object passed to inspect(options) with the following optional properties:

  • iframe (function or iframe Element or false) - resolves to the iframe element to display the inspector in. If this is set to iframe: false, then a popup window will be used instead.

    ⚠️ Note: you might need to allow popups to display the inspector in a popup window, as they might be blocked by the browser by default.

    By default, the inspector will look for an <iframe data-xstate> element anywhere in the document. If you want to target a custom iframe, specify it eagerly or lazily:

    // eager
    inspect({
      iframe: document.querySelector('iframe.some-xstate-iframe')
    });
    // lazy
    inspect({
      iframe: () => document.querySelector('iframe.some-xstate-iframe')
    });
  • url (string) - the URL of the inspector to connect to. By default, the inspector is running on http://statecharts.io/inspect.

Returns: an inspector object with the following properties:

  • disconnect (function) - a function that disconnects the inspector and cleans up any listeners.

Implementing

You can implement your own inspector by creating a receiver. A receiver is an actor that receives inspector events from a source (like a parent window or a WebSocket connection):

  • "service.register"

    {
      type: 'service.register';
      machine: StateMachine<any, any, any>;
      state: State<any, any>;
      id: string;
      sessionId: string;
      parent?: string;
      source?: string;
    }
  • "service.stop"

    {
      type: 'service.stop';
      sessionId: string;
    }
  • "service.state"

    {
      type: 'service.state';
      state: State<any, any>;
      sessionId: string;
    }
  • "service.event"

    {
      type: 'service.event';
      event: SCXML.Event<any>;
      sessionId: string
    };

To listen to events from an inspected source, create a receiver with the appropriate create*Receiver(...) function; for example:

import { createWindowReceiver } from '@xstate/inspect';

const windowReceiver = createWindowReceiver(/* options? */);

windowReceiver.subscribe((event) => {
  // here, you will receive "service.*" events
  console.log(event);
});

You can also send events to the receiver:

// ...

// This will send the event to the inspected service
windowReceiver.send({
  type: 'xstate.event',
  event: JSON.stringify({ type: 'someEvent' }),
  service: /* session ID of the service this event is sent to */
});

The typical inspection workflow is as follows:

  1. The inspect(/* ... */) call on the client opens the inspector (e.g., in a separate window, or creates a WebSocket connection)
  2. The receiver sends an "xstate.inspecting" event to the client
  3. The client sends "service.register" events to the receiver
  4. An inspector listening to the receiver (via receiver.subscribe(...)) registers the machine (event.machine) by its event.sessionId
  5. The machine is visually rendered, and its current state (event.state) is highlighted
  6. As the service at the source receives events and changes state, it will send the receiver "service.event" and "service.state" events, respectively
  7. The inspector can use those events to highlight the current state and keep a log of events sent to that service
  8. When the service stops, a "service.stop" event is sent to the receiver with the event.sessionId to identify the stopped service.

FAQs

  • How do I run the inspector in a NextJS app?

    Ensure that the inspector code only runs on the client, rather than the server:

    if (typeof window !== 'undefined') {
      inspect({
        /* options */
      });
    }