sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
77.95k stars 4.07k forks source link

[Feature Request Svelte4] Use labels for mount and destroy callbacks instead of functions. #8265

Closed TheCymaera closed 1 year ago

TheCymaera commented 1 year ago

Describe the problem

Lifecycle callbacks are currently registered using functions that are imported from the svelte runtime.

import { onDestroy } from "svelte";
onDestroy(()=>{
    // do clean up here...
});

Describe the proposed solution

It would be more concise to use labels, similar to reactive statements. It may also more performant since the callbacks are known at compile-time.

onDestroy: {
    // do clean up here...
}

Alternatives considered

If the Explicit Resource Management API is accepted, they could be used for cleanup as well.

use timer = createTimer(callback);

Importance

nice to have

jrmoynihan commented 1 year ago

I see a couple thumbs down, but haven't seen any reasoning why this wouldn't be beneficial. I'm all for more terse syntax for the same functionality.

Is there anything potentially lost by using a labeled statement (that gets compiled to a function) instead of using the lecture funding we have now?

brunnerh commented 1 year ago
baseballyama commented 1 year ago

AFAIK we can not declare types for label statements at least without a hack so it may be weak for types. And if we support the label statement for that, we definitely need to drop exist onDestroy function because we should have 1 way to do this. But it is more painful than benefits for users because users need to do migration tasks.

Regarding performance, I think Svelte is compiler, so we can do what we want to do.