-
-
Notifications
You must be signed in to change notification settings - Fork 521
Signals debug docs #1374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Signals debug docs #1374
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| --- | ||
| title: Signals Debug | ||
| description: A powerful debugging toolkit for Preact Signals that provides detailed console insights into signal updates, effects, and computed values. | ||
| --- | ||
|
|
||
| # Signals Debug | ||
|
|
||
| `@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. | ||
|
|
||
| --- | ||
|
|
||
| <toc></toc> | ||
|
|
||
| --- | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @preact/signals-debug | ||
| ``` | ||
|
|
||
| > :bulb: **Important**: Import this package at the root of your application to ensure all signals are tracked from the start. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| Simply import the package at your application's entry point: | ||
|
|
||
| ```jsx | ||
| // index.jsx or main.jsx | ||
| import '@preact/signals-debug'; | ||
| import { signal, computed, effect } from '@preact/signals'; | ||
|
|
||
| // All signal operations will now be logged to the console | ||
| const count = signal(0); | ||
| const doubled = computed(() => count.value * 2); | ||
|
|
||
| effect(() => { | ||
| console.log('Count is:', count.value); | ||
| }); | ||
|
|
||
| count.value = 5; // This will be logged by the debug package | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| The debug package provides comprehensive tracking for all signal operations: | ||
|
|
||
| - **Value Changes**: Tracks and logs all signal value changes with before/after comparisons | ||
| - **Effect Tracking**: Monitors effect executions and their dependencies | ||
| - **Computed Values**: Tracks computed value recalculations and their dependencies | ||
| - **Update Grouping**: Groups related updates for better visualization in the console | ||
| - **Dependency Graph**: Tracks signal dependencies for visualization in DevTools | ||
|
|
||
| ## Configuration | ||
|
|
||
| Use `setDebugOptions()` to configure debugging behavior: | ||
|
|
||
| ```typescript | ||
| import { setDebugOptions } from '@preact/signals-debug'; | ||
|
|
||
| setDebugOptions({ | ||
| // Group related updates in console output (default: true) | ||
| grouped: true, | ||
|
|
||
| // Enable/disable debugging entirely (default: true) | ||
| enabled: true, | ||
|
|
||
| // Enable/disable console logging (default: true) | ||
| consoleLogging: true, | ||
|
|
||
| // Number of spaces for nested update indentation (default: 2) | ||
| // Useful for non-browser environments | ||
| spacing: 2, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Disabling Console Output | ||
|
|
||
| If you're using the DevTools extension and don't want duplicate console output: | ||
|
|
||
| ```typescript | ||
| import { setDebugOptions } from '@preact/signals-debug'; | ||
|
|
||
| // Disable console logging, keep DevTools integration | ||
| setDebugOptions({ | ||
| consoleLogging: false, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Signal Naming | ||
|
|
||
| For more meaningful debug output, you can name your signals using the `name` option: | ||
|
|
||
| ```typescript | ||
| import { signal, computed } from '@preact/signals'; | ||
|
|
||
| const count = signal(0, { name: 'count' }); | ||
| const doubled = computed(() => count.value * 2, { name: 'doubled' }); | ||
| ``` | ||
|
|
||
| ### Automatic Naming with Babel Transforms | ||
|
|
||
| For automatic signal naming in development, use the appropriate Babel transform for your framework: | ||
|
|
||
| #### Preact | ||
|
|
||
| ```bash | ||
| npm install --save-dev @preact/signals-preact-transform | ||
| ``` | ||
|
|
||
| ```js | ||
| // babel.config.js | ||
| module.exports = { | ||
| plugins: [['module:@preact/signals-preact-transform']], | ||
| }; | ||
| ``` | ||
|
|
||
| #### React | ||
|
|
||
| ```bash | ||
| npm install --save-dev @preact/signals-react-transform | ||
| ``` | ||
|
|
||
| ```js | ||
| // babel.config.js | ||
| module.exports = { | ||
| plugins: [ | ||
| [ | ||
| '@preact/signals-react-transform', | ||
| { | ||
| mode: 'auto', | ||
| experimental: { | ||
| // Enable automatic naming for debugging | ||
| debug: true, | ||
| }, | ||
| }, | ||
| ], | ||
| ], | ||
| }; | ||
| ``` | ||
|
|
||
| Both transforms automatically add names based on variable names: | ||
|
|
||
| ```typescript | ||
| // Before transform | ||
| const count = signal(0); | ||
|
|
||
| // After transform (conceptually) | ||
| const count = signal(0, { name: 'count' }); | ||
| ``` | ||
|
|
||
| ## DevTools Integration | ||
|
|
||
| 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: | ||
|
|
||
| - Receive real-time signal updates | ||
| - Track signal disposal events | ||
| - Configure debug settings remotely | ||
| - Build dependency graphs | ||
|
|
||
| For embedded debugging UI, see the [DevTools UI documentation](/guide/v10/signals-devtools-ui). | ||
|
|
||
| ## Related Packages | ||
|
|
||
| - [`@preact/signals`](/guide/v10/signals) - Core signals library for Preact | ||
| - [`@preact/signals-devtools-ui`](/guide/v10/signals-devtools) - Visual DevTools UI for debugging signals | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.