Skip to content
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

$inspect shows empty state object using Classes #14988

Open
hermit99 opened this issue Jan 12, 2025 · 4 comments
Open

$inspect shows empty state object using Classes #14988

hermit99 opened this issue Jan 12, 2025 · 4 comments

Comments

@hermit99
Copy link

hermit99 commented Jan 12, 2025

Describe the bug

If a Class contains only $state runes, e.g.

class A {
  sa = $state('$inspect shows empty')
}
export const a = new A()

$inspect shows an empty obj. Whereas if it contains methods, etc., $inspect has more information.

Is this expected?

Reproduction

Playground

Logs

No response

System Info

  System:
    OS: Windows 11 10.0.26100
  Binaries:
    Node: 22.11.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.3 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (131.0.2903.86)
    Internet Explorer: 11.0.26100.1882

Severity

annoyance

@Leonidaz
Copy link

Leonidaz commented Jan 13, 2025

looks it's happening in the clone function.

structuredClone fails if an instance contains a method but succeeds if no methods are defined. Svelte relies on the error to return the uncloned instance.

try {
return /** @type {Snapshot<T>} */ (structuredClone(value));
} catch (e) {
if (DEV) {
paths.push(path);
}
return /** @type {Snapshot<T>} */ (value);
}

not sure what can be done here. maybe just return the uncloned value without even attempting to clone it if it doesn't match any of the known types above (Data, object, Map, Set, etc.)?

Or, if $inspect could take a custom clone function that takes an input and provides a cloned output.

@paoloricciuti
Copy link
Member

Btw you can define a toJSON. method to specify how a class should be "snapshotted"

REPL

@hermit99
Copy link
Author

Good to know, thanks @paoloricciuti. However that damages the out-of-the-box brevity of $inspect?

@paoloricciuti
Copy link
Member

Good to know, thanks @paoloricciuti. However that damages the out-of-the-box brevity of $inspect?

Why? You just need to make sure to add a toJSON method when you create the class if you think you might use that in $inspect...calling $inspect will be as quick as before.

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

No branches or pull requests

3 participants