Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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 #685

Closed
2 tasks
dlindahl opened this issue Mar 1, 2023 · 3 comments
Closed
2 tasks
Labels
help wanted Extra attention is needed

Comments

@dlindahl
Copy link

dlindahl commented Mar 1, 2023

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.

@dai-shi
Copy link
Member

dai-shi commented Mar 1, 2023

Nice catch. Thanks for reporting.

Would you or anyone like to fix it? derive is already complicated, and hope to simplify the implementation.

@dai-shi dai-shi added the help wanted Extra attention is needed label Mar 1, 2023
@dai-shi
Copy link
Member

dai-shi commented Sep 27, 2023

Here's a new proposition, which might be controversial: #792

@dai-shi
Copy link
Member

dai-shi commented Nov 3, 2023

Transferred to: valtiojs/derive-valtio#1

@pmndrs pmndrs locked and limited conversation to collaborators Nov 3, 2023
@dai-shi dai-shi converted this issue into discussion #805 Nov 3, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants