Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/structure-rx-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solidjs/start-devtools': minor
---

The Structure view hosts the live map of a ReactiveScript app. When the app sets `globalThis.__RX__` with a `mount(element)` function, as `@reactivescript/map/live` does in development, a third chip, Map, mounts the map into the view and disposes it when you leave Map or close the dock. Without a map, the chip is not there, and a remembered Map falls back to Owners.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ The dock has three views, picked in the header:
- **Health**: thrown errors and the issues the runtime reports, in one list.
- **Activity**: one stream of actions, server-function calls, async loads and errors, with
a strip of lanes over time.
- **Structure**: the ownership tree, or the reactivity graph.
- **Structure**: the ownership tree, or the reactivity graph. In a ReactiveScript app it also
has a **Map** chip for the app's live map.

Press ⌘K (Ctrl+K) while the dock is open to find a component, signal, memo, action or
server function by name.
Expand Down Expand Up @@ -166,6 +167,18 @@ extra setup. Clicking it asks the dev server to open the file in your editor.
The panel reads the tree through the development hooks in `solid-js`, so it is empty in a
production build of the runtime. It only watches while it is open.

## ReactiveScript map

An app written in ReactiveScript can register a live map of its reactive graph in
development, by setting `globalThis.__RX__` to an object with a `mount(element)` function
that returns a dispose function. `@reactivescript/map/live` does this from the app's dev
entry. The Structure view then has a third chip, **Map**, which mounts the map into the
view, and lights the nodes each update changed.

The toolbar looks for `__RX__` each time the view opens, and takes the map out again when you
leave Map or close the dock. It does not depend on any ReactiveScript package. On a page with
no map, a remembered Map chip falls back to Owners.

## Copy for an agent

Each panel has a copy button that puts a Markdown report on the clipboard. Paste it into
Expand Down
9 changes: 7 additions & 2 deletions src/dev-toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const CommandPalette = clientOnly(() => import('./palette/index.js'), { lazy: tr

/** A view of the dock. */
type Tab = 'health' | 'activity' | 'structure';
type StructureMode = 'owners' | 'graph';
/** Map is offered only while the app has a ReactiveScript map. The view falls back to Owners. */
type StructureMode = 'owners' | 'graph' | 'map';

const VIEWS: { value: Tab; label: string }[] = [
{ value: 'health', label: 'Health' },
Expand Down Expand Up @@ -297,7 +298,11 @@ export function DevToolbar(props: DevToolbarProps) {
const saved = readDockState();
const restored = savedTab(saved.tab);
if (restored) setTab(restored);
if (saved.structureMode === 'owners' || saved.structureMode === 'graph') {
if (
saved.structureMode === 'owners' ||
saved.structureMode === 'graph' ||
saved.structureMode === 'map'
) {
setStructureMode(saved.structureMode);
}
void loadSolidVersion().then((value) => {
Expand Down
114 changes: 88 additions & 26 deletions src/dev-toolbar/structure/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { JSX } from '@solidjs/web';
import { createSignal, For } from 'solid-js';
import { createEffect, createMemo, createSignal, For, onSettled, Show } from 'solid-js';
import OwnershipViewer, { type SignalChange } from '../ownership/index.js';
import ReactivityViewer from '../reactivity/index.js';
import {
mountRxMap,
readRxMapHost,
type RxMapHost,
shownStructureMode,
type StructureMode,
stepStructureMode,
structureModes,
} from './rx-map.js';
import './styles.css';

export type StructureMode = 'owners' | 'graph';

export type { SignalChange };
export type { RxMapHost, SignalChange, StructureMode };

export interface StructureViewerProps {
show?: boolean;
/** Which of the two the view shows. Leave it out to let the view keep its own. */
/**
* Which pane the view shows. Leave it out to let the view keep its own. Map
* falls back to Owners while the app has no ReactiveScript map.
*/
mode?: StructureMode;
onModeChange?: (mode: StructureMode) => void;
/** A signal, memo or effect another panel asked the graph to show. */
Expand All @@ -32,68 +42,117 @@ export interface StructureViewerProps {
onShowActivity?: (owner: object, name: string) => void;
}

const MODES: { value: StructureMode; label: string; name: string }[] = [
{ value: 'owners', label: 'Owners', name: 'Ownership' },
{ value: 'graph', label: 'Graph', name: 'Reactivity' },
];
const MODES: Record<StructureMode, { label: string; name: string }> = {
owners: { label: 'Owners', name: 'Ownership' },
graph: { label: 'Graph', name: 'Reactivity' },
map: { label: 'Map', name: 'ReactiveScript map' },
};

interface ModeChipsProps {
modes: StructureMode[];
mode: StructureMode;
onChange: (mode: StructureMode) => void;
}

/** The Owners and Graph chips. Both sides of the view show them. */
/** The Owners, Graph and Map chips. Every pane of the view shows them. */
function ModeChips(props: ModeChipsProps): JSX.Element {
function moveMode(event: KeyboardEvent): void {
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
event.preventDefault();
props.onChange(props.mode === 'owners' ? 'graph' : 'owners');
// Each pane has its own chips, so the pane that takes over is searched from
// the dock body: this chip leaves the page with its pane.
const chip = event.currentTarget as HTMLElement | null;
const body = chip?.closest('[data-solid-dev-toolbar-body]') ?? chip?.parentElement;
props.onChange(stepStructureMode(props.modes, props.mode, event.key === 'ArrowLeft' ? -1 : 1));
// The chip that takes over is the one that is now selected.
queueMicrotask(() => {
(event.currentTarget as HTMLElement | null)?.parentElement
?.querySelector<HTMLElement>('[aria-selected="true"]')
body
?.querySelector<HTMLElement>('[data-solid-structure-chip][aria-selected="true"]')
?.focus();
});
}

return (
<div data-solid-structure-modes role="tablist" aria-label="Structure">
<For each={MODES}>
{(entry) => (
<For each={props.modes}>
{(value) => (
<button
type="button"
role="tab"
data-solid-structure-chip
data-solid-structure-mode={entry.value}
aria-label={entry.name}
aria-selected={props.mode === entry.value ? 'true' : 'false'}
tabindex={props.mode === entry.value ? 0 : -1}
onClick={() => props.onChange(entry.value)}
data-solid-structure-mode={value}
aria-label={MODES[value].name}
aria-selected={props.mode === value ? 'true' : 'false'}
tabindex={props.mode === value ? 0 : -1}
onClick={() => props.onChange(value)}
onKeyDown={moveMode}
>
{entry.label}
{MODES[value].label}
</button>
)}
</For>
</div>
);
}

interface RxMapPaneProps {
host: RxMapHost;
lead: JSX.Element;
}

/**
* The live map of a ReactiveScript app. The app's `__RX__.mount` draws it into
* the frame once the pane is on the page, and the pane takes it out again when
* it closes.
*/
function RxMapPane(props: RxMapPaneProps): JSX.Element {
let frame: HTMLDivElement | undefined;
const host = props.host;
onSettled(() => (frame ? mountRxMap(host, frame) : undefined));

return (
<div data-solid-dev-toolbar-panel>
<div data-solid-rx-map-viewer>
<div data-solid-rx-map-nav>{props.lead}</div>
<div
data-solid-rx-map-frame
ref={(element) => {
frame = element;
}}
/>
</div>
</div>
);
}

/**
* The Structure view: the ownership tree and the reactivity graph behind one
* pair of chips. Selecting a chip swaps the pane, and each pane keeps its own
* selection while it is hidden.
* The Structure view: the ownership tree and the reactivity graph behind a row
* of chips, plus the ReactiveScript map when the app registered one. Selecting
* a chip swaps the pane, and the tree and the graph keep their own selection
* while they are hidden.
*/
export default function StructureViewer(props: StructureViewerProps): JSX.Element {
const [ownMode, setOwnMode] = createSignal<StructureMode>('owners');
const mode = () => props.mode ?? ownMode();

// The app's dev entry registers the map, usually before the toolbar mounts.
// It is read again each time the view opens, in case it came later.
const [mapHost, setMapHost] = createSignal<RxMapHost | undefined>(readRxMapHost());
createEffect(
() => !!props.show,
(shown) => {
if (shown) setMapHost(readRxMapHost());
},
);

const modes = createMemo(() => structureModes(!!mapHost()));
const mode = () => shownStructureMode(props.mode ?? ownMode(), !!mapHost());

function changeMode(next: StructureMode): void {
setOwnMode(next);
props.onModeChange?.(next);
}

const chips = () => <ModeChips mode={mode()} onChange={changeMode} />;
const chips = () => <ModeChips modes={modes()} mode={mode()} onChange={changeMode} />;

return (
<>
Expand All @@ -114,6 +173,9 @@ export default function StructureViewer(props: StructureViewerProps): JSX.Elemen
onViewOwner={props.onViewOwner}
lead={chips()}
/>
<Show when={props.show && mode() === 'map' && mapHost()} keyed>
{(host) => <RxMapPane host={host} lead={chips()} />}
</Show>
</>
);
}
86 changes: 86 additions & 0 deletions src/dev-toolbar/structure/rx-map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, expect, it, vi } from 'vitest';
import {
mountRxMap,
readRxMapHost,
type RxMapHost,
shownStructureMode,
stepStructureMode,
structureModes,
} from './rx-map.js';

const el = {} as HTMLElement;

describe('readRxMapHost', () => {
it('finds a host with a mount function', () => {
const host = { graphs: [], mount: () => () => {} };
expect(readRxMapHost({ __RX__: host })).toBe(host);
});

it('ignores a missing host, or one without mount', () => {
expect(readRxMapHost({})).toBeUndefined();
expect(readRxMapHost({ __RX__: null })).toBeUndefined();
expect(readRxMapHost({ __RX__: { graphs: [] } })).toBeUndefined();
expect(readRxMapHost({ __RX__: { mount: 'no' } })).toBeUndefined();
});

it('ignores a getter that throws', () => {
const scope = {
get __RX__() {
throw new Error('blocked');
},
};
expect(readRxMapHost(scope)).toBeUndefined();
});
});

describe('structure modes', () => {
it('offers Map only when the app has a map', () => {
expect(structureModes(false)).toEqual(['owners', 'graph']);
expect(structureModes(true)).toEqual(['owners', 'graph', 'map']);
});

it('shows Owners for a saved Map mode when the app has no map', () => {
expect(shownStructureMode('map', false)).toBe('owners');
expect(shownStructureMode('map', true)).toBe('map');
expect(shownStructureMode('graph', false)).toBe('graph');
});

it('steps through the modes it offers, wrapping around', () => {
const modes = structureModes(true);
expect(stepStructureMode(modes, 'graph', 1)).toBe('map');
expect(stepStructureMode(modes, 'map', 1)).toBe('owners');
expect(stepStructureMode(modes, 'owners', -1)).toBe('map');
expect(stepStructureMode(structureModes(false), 'graph', 1)).toBe('owners');
});
});

describe('mountRxMap', () => {
it('mounts into the element and disposes once', () => {
const dispose = vi.fn();
const host: RxMapHost = { mount: vi.fn(() => dispose) };
const unmount = mountRxMap(host, el);
expect(host.mount).toHaveBeenCalledWith(el);
expect(dispose).not.toHaveBeenCalled();
unmount();
unmount();
expect(dispose).toHaveBeenCalledTimes(1);
});

it('survives a map that throws', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const failing: RxMapHost = {
mount: () => {
throw new Error('no map');
},
};
expect(() => mountRxMap(failing, el)()).not.toThrow();
const throwing: RxMapHost = {
mount: () => () => {
throw new Error('no dispose');
},
};
expect(() => mountRxMap(throwing, el)()).not.toThrow();
expect(warn).toHaveBeenCalledTimes(2);
warn.mockRestore();
});
});
Loading
Loading