Skip to content

feat: provide guidance in browser console when logging $state objects #13142

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 4 commits into from
Sep 16, 2024

Conversation

dummdidumm
Copy link
Member

Wrap console.log/warn/error statements in DEV mode with a check whether or not they contain state objects. Closes #13123

This is an alternative or enhancement to #13070. Alternative if we deem it the better solution. Enhancement because it's not as robust as a custom formatter: We only check the top level of each entry (though we could maybe traverse a few levels), and if you're logging class instances, snapshot currently stops at the boundaries there and so you don't get snapshotted values for these (arguably this is a more general problem of $inspect and $state.snapshot), whereas with custom formatter it doesn't matter at which level you come across it.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Wrap console.log/warn/error statements in DEV mode with a check whether or not they contain state objects. Closes #13123

This is an alternative or enhancement to #13070. Alternative if we deem it the better solution. Enhancement because it's not as robust as a custom formatter: We only check the top level of each entry (though we could maybe traverse a few levels), and if you're logging class instances, snapshot currently stops at the boundaries there and so you don't get snapshotted values for these (arguably this is a more general problem of $inspect and $state.snapshot), whereas with custom formatter it doesn't matter at which level you come across it.
Copy link

changeset-bot bot commented Sep 5, 2024

🦋 Changeset detected

Latest commit: ec9ea5d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trueadm
Copy link
Contributor

trueadm commented Sep 5, 2024

I think we need this in addition to the formatter as for many people that use Chrome, the experience of using the formatter will exceed the experience of anything else. This can serve as a nice fallback for those on Safari.

@Rich-Harris Rich-Harris merged commit ed7611b into main Sep 16, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the state-logging branch September 16, 2024 21:52
@brunnerh
Copy link
Member

brunnerh commented Sep 16, 2024

@dummdidumm
I think this is missing something like an untrack.
$effects now fire that otherwise would not.
REPL

dummdidumm added a commit that referenced this pull request Sep 17, 2024
Rich-Harris added a commit that referenced this pull request Sep 17, 2024
* fix: ensure snapshot logs don't affect dependency graph

untrack the whole function
See #13142 (comment)

* try-catch

* appease eslint

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Svelte5] Very strange behavior with a $state() created by a $state.snapshot()
4 participants