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

Updating prop values of an object with a function property causes an exception #194

Closed
davidjayb opened this issue Apr 1, 2024 · 3 comments · Fixed by #195
Closed

Updating prop values of an object with a function property causes an exception #194

davidjayb opened this issue Apr 1, 2024 · 3 comments · Fixed by #195
Labels
bug Issue that confirms something is incorrect

Comments

@davidjayb
Copy link
Contributor

davidjayb commented Apr 1, 2024

Describe the bug
When trying to update a value in an object with a function property, if the function is called in the page or component it will throw an exception that the function does not exist. Here is an example updating an object in an array:

Uncaught (in promise) TypeError: ctx[1].<function_name> is not a function
    update <component_name>.svelte:25
    p courier.js:264
    update <component_name>.svelte:510
    p courier.js:264
    update scheduler.js:119
    flush scheduler.js:79
    promise callback*schedule_update scheduler.js:20
    make_dirty Component.js:81
    ctx Component.js:139
    $inject_state <component_name>.svelte:612
    __svelte_devtools_inject_state courier.js:369
    <anonymous> - eval code:1

To Reproduce
Steps to reproduce the behavior:

  1. Create a component that accepts props that includes a class (or base object) with functions.
  2. Reference the function in the component.
  3. Update a prop value in the dev tools.

Expected behavior
The update should succeed and the function should be intact.

Environment

  • Firefox 124.0.1
  • built from master
  • Svelte 4.2.11

Additional context

This appears to be due, in part, to how objects are cloned for display in the devtools chrome:

https://github.com/sveltejs/svelte-devtools/blob/master/src/client/index.js#L99

Functions need to be turned into a string form. Furthermore, the clone does will not include object functions in serialization if they inherited from a class.

I also noticed this issue if object references such as URL are made in a component:

$: search = $page.url.searchParams.get('search');

Updating the component will produce an error because the url will no longer have a searchParams property.

@ignatiusmb
Copy link
Member

Thank you for the report, I think I get the sense of what the problem and solution is. Can you provide a minimal repro though, I couldn't seem to get it consistently or even at all.

@ignatiusmb ignatiusmb added the bug Issue that confirms something is incorrect label Apr 2, 2024
@davidjayb
Copy link
Contributor Author

davidjayb commented Apr 5, 2024

Here is the test bed:
https://github.com/davidjayb/svelte-devtools-194

With the devtools, you can notice the class function returning undefined if you update the prop:
https://github.com/davidjayb/svelte-devtools-194/blob/main/src/routes/%2Bpage.svelte#L16
https://github.com/davidjayb/svelte-devtools-194/blob/main/src/routes/%2Bpage.svelte#L24
https://github.com/davidjayb/svelte-devtools-194/blob/main/src/routes/%2Bpage.svelte#L32

Screenshot 2024-04-05 at 9 08 56 AM

To receive the issue with the $page.url.searchParams is a bit trickier to encounter. On the page /search update the prop from the Root component.
Screenshot 2024-04-05 at 9 07 52 AM
Screenshot 2024-04-05 at 9 08 06 AM

After updating the PR repo to the latest master it seems updating props has broken. However once that is fixed you should be able to test things out.

@ignatiusmb
Copy link
Member

Thank you for the detailed repro! I ended up reworking how the inject function is called in #205. There's still some edge cases with classes and getters, which I'll continue working in #195

@ignatiusmb ignatiusmb linked a pull request Apr 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue that confirms something is incorrect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants