fix: access last safe value of prop on unmount #15342
Closed
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.
Closes #14725
Draft for the moment since i need to write proper tests for this but this seems to cover most cases. Basically when there's a situation that could lead to a component being unmounted (if or dynamic component) we inject a
safe_props
function call that proxifies the identifiers that are passed to components creating a derived for each accessed value and storing the old value in a map. We also register a teardown that set anunmounting
flag to true so that we know when we are unmounting...in that case the derived returns the old and safe value instead.There might be some other cases I'm not considering...i've successfully tested it with this if you want to try and break it.
with
Component.svelte
beingwe should also check this doesn't add too much overhead.
P.s. another possible side effect is that to store the
old
value initially we read every prop in untrack...this should be fine since they should all be identifiers but if they are getters users might get an extra undetected call.Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint