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/quiet-devtools-reads.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solidjs/start-devtools': patch
---

The devtools' own reads of the app no longer fill the Health view with `STRICT_READ_UNTRACKED` warnings about `_flags`, `_time`, `_subs` and `_config`. The graph and the tree read node fields, and on a store those reads went through its proxy. Inside a strict-read scope of the app, such as an effect callback, the runtime reported each one as the app's. The devtools now read the app untracked, and Health leaves out anything the runtime reports while they do.
15 changes: 12 additions & 3 deletions src/dev-toolbar/dev-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DEV } from 'solid-js';
import { quietly } from './quiet.js';

/** The dev hooks of solid-js the toolbar uses. */
export interface DevHooksSlot {
Expand Down Expand Up @@ -37,17 +38,25 @@ export function createDevHooksHub(slot: DevHooksSlot): DevHooksHub {
onUpdate: slot.onUpdate,
};
original = found;
// The hooks run inside the app, often in a component body, so the
// listeners read quietly: what they read is not the app's doing.
slot.onOwner = (owner) => {
found.onOwner?.(owner);
for (const listener of listeners) listener.onOwner?.(owner);
quietly(() => {
for (const listener of listeners) listener.onOwner?.(owner);
});
};
slot.onGraph = (value, owner) => {
found.onGraph?.(value, owner);
for (const listener of listeners) listener.onGraph?.(value, owner);
quietly(() => {
for (const listener of listeners) listener.onGraph?.(value, owner);
});
};
slot.onUpdate = () => {
found.onUpdate?.();
for (const listener of listeners) listener.onUpdate?.();
quietly(() => {
for (const listener of listeners) listener.onUpdate?.();
});
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/dev-toolbar/issues/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OBSERVE } from 'solid-js';
import { isQuiet } from '../quiet.js';

/** One finding of the runtime's diagnostics channel, as `OBSERVE.diagnostics` delivers it. */
export interface IssueEvent {
Expand Down Expand Up @@ -118,6 +119,8 @@ export function captureIssues(
if (!diagnostics) return () => {};
let pending: { event: IssueEvent; subject: object | undefined; at: number }[] = [];
return diagnostics.subscribe((event, subject) => {
// The devtools' own reads of the app are not the app's findings.
if (isQuiet()) return;
if (!pending.length) {
queueMicrotask(() => {
const batch = pending;
Expand Down
4 changes: 4 additions & 0 deletions src/dev-toolbar/ownership/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEV } from 'solid-js';
import { listenToDevHooks } from '../dev-hooks.js';
import { quietly } from '../quiet.js';
import { isSolidBuiltin } from './builtins.js';
import { buildOwnershipTree, EMPTY_TREE, type OwnershipTree, type RawNode } from './tree.js';

Expand Down Expand Up @@ -172,7 +173,10 @@ export interface SnapshotOptions {
*/
export function snapshotOwnershipTree(options?: SnapshotOptions): OwnershipTree {
if (!isOwnershipAvailable()) return EMPTY_TREE;
return quietly(() => readTree(options));
}

function readTree(options?: SnapshotOptions): OwnershipTree {
const roots = new Set<RawNode>();
if (seedOwner) roots.add(rootOf(seedOwner));
for (const ref of tracked) {
Expand Down
78 changes: 78 additions & 0 deletions src/dev-toolbar/quiet.dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
createEffect,
createRoot,
createSignal,
createStore,
flush,
OBSERVE,
untrack,
} from 'solid-js';
import { afterEach, describe, expect, it } from 'vitest';
import { captureIssues, type IssueEvent } from './issues/tracker.js';
import { snapshotOwnershipTree, startOwnershipTracking } from './ownership/registry.js';
import { quietly } from './quiet.js';
import { snapshotReactivityGraph, startReactivityTracking } from './reactivity/registry.js';

// The label the runtime gives an untracked read it warns about, as in a component body.
const COMPONENT_BODY = 'a component body';

function strictReads(): string[] {
const names: string[] = [];
stops.push(
OBSERVE!.diagnostics.subscribe((event) => {
if (event.code === 'STRICT_READ_UNTRACKED') names.push(event.nodeName ?? '');
}),
);
return names;
}

const stops: (() => void)[] = [];
afterEach(() => {
while (stops.length) stops.pop()!();
});

describe('the devtools reading the app', () => {
it('raise no strict-read findings, even in an app effect callback', () => {
stops.push(startReactivityTracking(), startOwnershipTracking());
const reads = strictReads();
const [count, setCount] = createSignal(0);
createRoot((dispose) => {
stops.push(dispose);
untrack(() => {
// The runtime lists the store's proxy beside the plain signals.
const [state] = createStore({ items: [1, 2], label: 'x' });
createEffect(
() => count(),
() => {
// An effect callback is a strict-read scope. Reading the node fields
// through the store's proxy here used to report `_flags`, `_time`...
snapshotReactivityGraph();
snapshotOwnershipTree();
void state;
},
);
}, COMPONENT_BODY);
});
flush();
setCount(1);
flush();
expect(reads).toEqual([]);
});

it('leave what the runtime reports during a devtools read out of Health', async () => {
const [state] = createRoot(() => createStore({ label: 'x' }));
const captured: IssueEvent[] = [];
stops.push(captureIssues((added) => captured.push(...added.map((entry) => entry.event))));
const reads = strictReads();

// A strict-read scope opened inside a devtools read still reports to the
// console, but it is not the app's finding.
quietly(() => untrack(() => state.label, COMPONENT_BODY));
// The same read by the app is.
untrack(() => state.label, COMPONENT_BODY);
await Promise.resolve();

expect(reads).toEqual(['label', 'label']);
expect(captured.map((event) => event.nodeName)).toEqual(['label']);
});
});
30 changes: 30 additions & 0 deletions src/dev-toolbar/quiet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { untrack } from 'solid-js';

/**
* The devtools read the app's runtime nodes field by field, such as `_flags`
* and `_time`. A store hands out proxies, and the runtime lists them beside
* plain signals, so the same field reads go through the store's proxy. In a
* strict-read scope, such as an effect callback of the app, the runtime then
* reports `STRICT_READ_UNTRACKED` for every field, as if the app had read it.
*
* `untrack` is the runtime's own way to mark a deliberate one-time read: it
* closes the strict-read window. While it runs, the toolbar also leaves out
* whatever the runtime reports, so the Health view never counts findings the
* devtools caused.
*/
let depth = 0;

/** Runs a devtools read of the app untracked, with its findings left out of Health. */
export function quietly<T>(read: () => T): T {
depth++;
try {
return untrack(read);
} finally {
depth--;
}
}

/** Whether a devtools read is running now. */
export function isQuiet(): boolean {
return depth > 0;
}
1 change: 1 addition & 0 deletions src/dev-toolbar/reactivity/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vi.mock('solid-js', () => ({
get DEV() {
return runtime.DEV;
},
untrack: <T>(read: () => T): T => read(),
}));

describe('registry without the dev runtime', () => {
Expand Down
15 changes: 11 additions & 4 deletions src/dev-toolbar/reactivity/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEV } from 'solid-js';
import { listenToDevHooks } from '../dev-hooks.js';
import { quietly } from '../quiet.js';

// Flag bits used by @solidjs/signals. They are internal to the runtime, so the
// values are copied here and every read is defensive.
Expand Down Expand Up @@ -186,10 +187,12 @@ export function startReactivityTracking(): () => void {
*/
function recordUpdates(): void {
if (tracked.size > STATS_LIMIT) return;
for (const ref of tracked) {
const node = ref.deref();
if (node) statsOf(node);
}
quietly(() => {
for (const ref of tracked) {
const node = ref.deref();
if (node) statsOf(node);
}
});
}

/** Drops one watcher. Stops listening once nothing watches any more. */
Expand Down Expand Up @@ -420,6 +423,10 @@ export interface SnapshotOptions {
*/
export function snapshotReactivityGraph(options?: SnapshotOptions): ReactiveGraph {
if (!isReactivityAvailable()) return EMPTY_GRAPH;
return quietly(() => readGraph(options));
}

function readGraph(options?: SnapshotOptions): ReactiveGraph {
const includeDisposed = options?.includeDisposed ?? false;

const queue: RawNode[] = [];
Expand Down
Loading