Skip to content

fix: Add check for onStart and improve valueSetter implementation#8986

Open
MatiPl01 wants to merge 6 commits intomainfrom
@matipl01/refine-valueSetter-implementation
Open

fix: Add check for onStart and improve valueSetter implementation#8986
MatiPl01 wants to merge 6 commits intomainfrom
@matipl01/refine-valueSetter-implementation

Conversation

@MatiPl01
Copy link
Member

@MatiPl01 MatiPl01 commented Feb 19, 2026

Summary

This PR improves the valueSetter in terms of:

  • code quality and readability (less nesting, better code separation),
  • TS usage (removes multiple type casts),
  • implementation safety (added check for onStart presence in the AnimationObject (see Crash in valueSetter: animation.onStart is undefined (useDerivedValue) #8974) and adds a check whether the value returned by the animation factory function is really an AnimationObject - before we just checked if a value is a function and assumed that the value it returns is AnimationObject without performing any additional checks)

@MatiPl01 MatiPl01 self-assigned this Feb 19, 2026
@MatiPl01 MatiPl01 linked an issue Feb 19, 2026 that may be closed by this pull request
2 tasks
@MatiPl01 MatiPl01 requested a review from tomekzaw February 19, 2026 02:04
@tomekzaw tomekzaw requested a review from tjzel February 19, 2026 07:13
@MatiPl01 MatiPl01 force-pushed the @matipl01/refine-valueSetter-implementation branch from dffb4c0 to 8492e05 Compare February 19, 2026 13:44
@MatiPl01 MatiPl01 requested a review from tjzel February 19, 2026 13:44
Comment on lines +29 to +30
'onFrame' in value &&
'onStart' in value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a question - I looked into the issue you linked about missing onStart - where do we ever have a path when an animation object has onFrame but doesn't have onStart?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I just added this check as it seemed to be reasonable. Maybe when someone assigns a custom function to the shared value which has onFrame field but not onStart but it is a very unlikely case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should find it first to understand the issue instead of patching it with more if conditions.

@MatiPl01 MatiPl01 requested a review from tjzel February 19, 2026 14:39
Comment on lines +20 to +26
const valueObject =
typeof unwrappedValue === 'object' && unwrappedValue !== null
? (unwrappedValue as Record<string, unknown>)
: {};

// Check if the value returned by the factory function is not an AnimationObject
if (valueObject.onFrame === undefined || valueObject.onStart === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks confusing. If someone assigned i.e. a number to a shared value, which invoked the value setter, we then create an empty object and check if it has onFrame or onStart properties.

How about something like

Suggested change
const valueObject =
typeof unwrappedValue === 'object' && unwrappedValue !== null
? (unwrappedValue as Record<string, unknown>)
: {};
// Check if the value returned by the factory function is not an AnimationObject
if (valueObject.onFrame === undefined || valueObject.onStart === undefined) {
const isAnimationObject =
typeof unwrappedValue === 'object' && unwrappedValue !== null && unwrappedValue.onFrame;
if(!isAnimationObject)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid multiple type casts to Record<string, unknown>. Since we cannot use a type guard function, I decided to create a valueObject variable.

Unfortunately, TS won't allow use to access the onFrame prop on the unwrappedValue as long as its resolved type is just object.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to excessively cast unfortunately. More TS-ish ways will hurt performance

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take another look? I think it is more readable and doesn't overuse TS type casts.

@MatiPl01 MatiPl01 requested a review from tjzel February 19, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in valueSetter: animation.onStart is undefined (useDerivedValue)

2 participants