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

underive does not delete the derived property unless there is a dependency defined #1

Open
2 tasks
dai-shi opened this issue Nov 3, 2023 · 0 comments
Open
2 tasks

Comments

@dai-shi
Copy link
Member

dai-shi commented Nov 3, 2023

Transferred from: pmndrs/valtio#685 by @dlindahl


Summary

Calling underive with delete: true does not delete the derived property value unless there is a dependency.

  1. Calling derive subscribes to the dependencies defined within the derive function.
  2. Calling underive with delete: true loops through all of the dependencies.
  3. After removing each dependency subscription, the key of the subscription is added to a Set to delete later.
  4. Once each subscription has been removed, the derived functions that matched a subscription key are deleted from the proxyObject.

The problem arises from a situation where the derived function never calls get (for whatever reason) which prevents a subscription from being created. The lack of a subscription prevents the property from being deleted which then prevents new calls to derive because the property is already defined.

Is this silly? Yes.
Was this hard to debug. Also yes. 😄

Property deletion should probably be decoupled from the existence of a subscription, especially since I see some async subscriptions being handled in the derive code which could possibly introduce a situation where underive is called before the async value resolves which would result in a similar error.

Link to reproduction

derive({
  myProperty(get) {
    return 123; // NOTE: Not using `get` which does not create a dependency.
  }
}, {
  proxy: myProxy
})

underive(myProxy, { delete: true, keys: ['myProperty'])
// Throws Error: object property already defined

Pardon the lack of a codesandbox repro as the problem seemed pretty straight forward. I can make one if you really need one though.

Check List

Please do not ask questions in issues.

  • I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.

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

1 participant