Skip to content

Commit 89510d3

Browse files
committed
Signals debug docs
1 parent ecb8925 commit 89510d3

6 files changed

Lines changed: 1016 additions & 0 deletions

File tree

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: Signals Debug
3+
description: A powerful debugging toolkit for Preact Signals that provides detailed console insights into signal updates, effects, and computed values.
4+
---
5+
6+
# Signals Debug
7+
8+
`@preact/signals-debug` is a development tool that helps you understand and debug your signal-based application by providing detailed console output about signal updates, effect executions, and computed value recalculations.
9+
10+
---
11+
12+
<toc></toc>
13+
14+
---
15+
16+
## Installation
17+
18+
```bash
19+
npm install @preact/signals-debug
20+
```
21+
22+
> :bulb: **Important**: Import this package at the root of your application to ensure all signals are tracked from the start.
23+
24+
## Quick Start
25+
26+
Simply import the package at your application's entry point:
27+
28+
```jsx
29+
// index.jsx or main.jsx
30+
import '@preact/signals-debug';
31+
import { signal, computed, effect } from '@preact/signals';
32+
33+
// All signal operations will now be logged to the console
34+
const count = signal(0);
35+
const doubled = computed(() => count.value * 2);
36+
37+
effect(() => {
38+
console.log('Count is:', count.value);
39+
});
40+
41+
count.value = 5; // This will be logged by the debug package
42+
```
43+
44+
## Features
45+
46+
The debug package provides comprehensive tracking for all signal operations:
47+
48+
- **Value Changes**: Tracks and logs all signal value changes with before/after comparisons
49+
- **Effect Tracking**: Monitors effect executions and their dependencies
50+
- **Computed Values**: Tracks computed value recalculations and their dependencies
51+
- **Update Grouping**: Groups related updates for better visualization in the console
52+
- **Dependency Graph**: Tracks signal dependencies for visualization in DevTools
53+
54+
## Configuration
55+
56+
Use `setDebugOptions()` to configure debugging behavior:
57+
58+
```typescript
59+
import { setDebugOptions } from '@preact/signals-debug';
60+
61+
setDebugOptions({
62+
// Group related updates in console output (default: true)
63+
grouped: true,
64+
65+
// Enable/disable debugging entirely (default: true)
66+
enabled: true,
67+
68+
// Enable/disable console logging (default: true)
69+
consoleLogging: true,
70+
71+
// Number of spaces for nested update indentation (default: 2)
72+
// Useful for non-browser environments
73+
spacing: 2,
74+
});
75+
```
76+
77+
### Disabling Console Output
78+
79+
If you're using the DevTools extension and don't want duplicate console output:
80+
81+
```typescript
82+
import { setDebugOptions } from '@preact/signals-debug';
83+
84+
// Disable console logging, keep DevTools integration
85+
setDebugOptions({
86+
consoleLogging: false,
87+
});
88+
```
89+
90+
## Signal Naming
91+
92+
For more meaningful debug output, you can name your signals using the `name` option:
93+
94+
```typescript
95+
import { signal, computed } from '@preact/signals';
96+
97+
const count = signal(0, { name: 'count' });
98+
const doubled = computed(() => count.value * 2, { name: 'doubled' });
99+
```
100+
101+
This produces clearer console output:
102+
103+
```
104+
count: 0 → 1
105+
└─ doubled: 0 → 2
106+
```
107+
108+
### Automatic Naming with Babel Transforms
109+
110+
For automatic signal naming in development, use the appropriate Babel transform for your framework:
111+
112+
#### Preact
113+
114+
```bash
115+
npm install --save-dev @preact/signals-preact-transform
116+
```
117+
118+
```js
119+
// babel.config.js
120+
module.exports = {
121+
plugins: [['module:@preact/signals-preact-transform']],
122+
};
123+
```
124+
125+
#### React
126+
127+
```bash
128+
npm install --save-dev @preact/signals-react-transform
129+
```
130+
131+
```js
132+
// babel.config.js
133+
module.exports = {
134+
plugins: [
135+
[
136+
'@preact/signals-react-transform',
137+
{
138+
mode: 'auto',
139+
experimental: {
140+
// Enable automatic naming for debugging
141+
debug: true,
142+
},
143+
},
144+
],
145+
],
146+
};
147+
```
148+
149+
Both transforms automatically add names based on variable names and file locations:
150+
151+
```typescript
152+
// Before transform
153+
const count = signal(0);
154+
155+
// After transform (conceptually)
156+
const count = signal(0, { name: 'count (MyComponent.jsx:5)' });
157+
```
158+
159+
## DevTools Integration
160+
161+
The debug package exposes a global API (`window.__PREACT_SIGNALS_DEVTOOLS__`) that the [Preact Signals DevTools extension](/guide/v10/signals-devtools-ui) and embedded DevTools UI can use to:
162+
163+
- Receive real-time signal updates
164+
- Track signal disposal events
165+
- Configure debug settings remotely
166+
- Build dependency graphs
167+
168+
For embedded debugging UI, see the [DevTools UI documentation](/guide/v10/signals-devtools-ui).
169+
170+
## Related Packages
171+
172+
- [`@preact/signals`](/guide/v10/signals) - Core signals library for Preact
173+
- [`@preact/signals-devtools-ui`](/guide/v10/signals-devtools) - Visual DevTools UI for debugging signals

0 commit comments

Comments
 (0)