Why can I return a function from onMount?

Quick question: I'm allowed to (as in "TypeScript allows me to") return a function from the function I declare inside onMount. The docs does not comment on this (https://docs.solidjs.com/reference/lifecycle/on-mount), so what does it do? When is the returned function run? Example:
onMount(() => {
const subscriptionID = props.plexer.subscribe(PLAYER_MOVE_EVENT, (event) => {
if (
event.playerID === props.backend.localPlayer.id
&& event.locationID === props.colonyLocation.locationID
) {
setUserIsHere(true);
}
});

return () => {
props.plexer.unsubscribe(subscriptionID);
}
})
onMount(() => {
const subscriptionID = props.plexer.subscribe(PLAYER_MOVE_EVENT, (event) => {
if (
event.playerID === props.backend.localPlayer.id
&& event.locationID === props.colonyLocation.locationID
) {
setUserIsHere(true);
}
});

return () => {
props.plexer.unsubscribe(subscriptionID);
}
})
10 Replies
REEEEE
REEEEE2mo ago
Nothing
REEEEE
REEEEE2mo ago
You could add the onCleanup to your onMount? I'm not sure if this will be added tbh
Fexelitche
FexelitcheOP2mo ago
Is that a thing? I.e.:
onMount(() => {
onCleanup(()=>{});
})
onMount(() => {
onCleanup(()=>{});
})
It feels wrong for some reason
REEEEE
REEEEE2mo ago
You can put onCleanup generally wherever
Fexelitche
FexelitcheOP2mo ago
Man. I thought I had an incling of how those tracking scopes where implemented. But not anymore. That's for sure
REEEEE
REEEEE2mo ago
fun fact, you can also do effects within effects
Fexelitche
FexelitcheOP2mo ago
Now, that's illegal Or. At least should be. Sounds ripe for OOM and other crashes
REEEEE
REEEEE2mo ago
Yeah probably, I haven't found a usecase for them but I'm pretty sure it's used in the wild by someone might even be used in solid-primitives somewhere
Fexelitche
FexelitcheOP2mo ago
People are so concerned with whether they could - they didn't stop to think on whether they should - Some guy. Probably from Jurassic Park
Want results from more Discord servers?
Add your server