-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Current component is not cleared after flush #4899
Comments
I did see #4259, but it is specifically about onMount, and this issue is much more generic than that. If you are going to leave this closed, perhaps you can edit #4259 to include the scope that this bug If |
That might be reasonable. Reopening. |
To anyone else waiting for #4909 to be merged, it is possible to work around this using the following code: import { set_current_component, tick } from 'svelte/internal';
const promise = tick();
const oldThen = promise.then;
promise.then = function (fn) {
oldThen.call(promise, () => {
fn();
set_current_component(null);
});
}; |
Without this, unpredictable crashes may occur in seemingly random places. This will be reverted as soon as sveltejs/svelte#4909 is merged.
That component now throws an exception at runtime in 3.25.0. |
Describe the bug
The current component is not being cleared at the end of the flush call. This means that lifecycle callbacks, like onMount, onDestroy, etc. are made available outside of Svelte code, with unpredictable effects (effectively operating on a random component, possibly one that is already destroyed).
This looks like a simple oversight. It looks like a call to
set_current_component(null)
is missing after this loop:svelte/src/runtime/internal/scheduler.ts
Lines 43 to 48 in ec3589e
To Reproduce
Expected behavior
I expected an exception to be thrown after a second (which it indeed does if you comment out the
first setTimeout). Instead, it logs the most recently-updated component, which is not at all guaranteed to be the current one.
Severity
I'm currently stuck on this with a package I'm maintaining. I have found a workaround (importing
tick
fromsvelte-internal
and then monkey-patchingtick().then
to reset the component after flush) but it is extremely hacky and I would not want to put this in a hosted library.Additional context
I'm maintaining a helper package which enables closer integration between Svelte and Meteor. It registers an onDestroy callback to handle cleanup of certain resources, but the function in question needs to be able to work both inside and outside Svelte components, hence the need to be able to trust that the current component is cleared.
The text was updated successfully, but these errors were encountered: