Proposal: If input is function, call it to use return value as data#801
Open
andrelandgraf wants to merge 1 commit intotracked-tools:mainfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey! I am not sure if you are currently considering feature proposals. This is just a small enhancement that I wish
TrackedAsyncDatawould support. Happy for any reviews/suggestions/support to get this merged. However, feel free to disregard if you think this is out-of-scope! :)Motivation
We use
TrackedAsyncData(222 results in 63 files), mostly to fetch data on mount of a component:We use a variety of patterns when the business logic becomes more complex, for example, returning different
TrackedAsyncDatafor each conditional path, then-chaining additional logic on the initial promise, or wrapping everything in anew Promise.TrackedAsyncDatareturn values:new Promise:All of these work perfectly fine. However, I wish you could pass
TrackedAsyncDataan inline callback function directly without the need to wrap it in anew Promiseor usethenchaining:Now, I have a co-located inline/nested function that can easily be moved out to be a private helper or moved to a service without needing any more refactoring. It's also super easy to read without any wrapping, then-chaining, or similar! Would love this to be a thing!
Proposed changes
Check
dataTrackedAsyncDataconstructor argument. If it is a function, call it to use the return value as the data.This is technically a breaking change because currently a passed-in function is used as the value (this actually happened to me on accident before). I can't think of a use case where this would be the desired behavior (have a function as a value of a
TrackedAsyncDataobject), but that behavior would technically break now. I updated thepackage.jsonversion tov2because of this, but I also think you have existing plans for av2update already. Happy to merge this into av2branch or follow any other recommendations!Tests
I added basic tests and type tests for the changes. Please let me know if I should add more.
Considerations
I think it would be reasonable to support this case but also would understand if you think the added scope and complexity is not worth adding this. Thank you for taking a look!