https://kotlinlang.org logo
Title
d

dave08

05/08/2023, 1:06 PM
Is there such a thing as a Resource that reinitializes itself if it's closed? I want to store some in a map, close them when unused, re-open them on a new usage.
s

simon.vergauwen

05/08/2023, 1:16 PM
close them when unused
No, that sounds highly mutable and the
Map
somehow needs to know when it goes out-of-scope.
Scala has the concept of
HotSwap
which I've also ported, but have not bothered bringing to Arrow. It's to basically open and use
Resource
on rotation. https://github.com/nomisRev/arrow-fx-coroutines-utils/blob/main/src/commonMain/kotlin/io/github/nomisrev/Hotswap.kt
d

dave08

05/08/2023, 1:22 PM
I have an Android app with a map of state machines handling the state of each item on a list. I was thinking of using resource to initialize and use them in a Compose view and unsubscribe/close them when they go out of view. But when they come back into view, I need to re-initialise them... yeah, I guess that's really mutable, but I'm not sure what I can do otherwise.
open and use
Resource
on rotation.
If I try to retreive this same resource from the map when it's closed, it'll re-open it?
s

simon.vergauwen

05/08/2023, 1:59 PM
Hmm, so each
@Composeable
has it's own
Resource
? A
@Composeable
can listen to when it gets destroyed, right? I guess there could be some integration there, using
Resource.allocated
🤔 I am not familiar enough with Compose, and it's been a long time since I did Android but it might be possible.
Something in
@Composeable
that doesn't gets retriggered on recomposition, and supports suspend. Is there something like
rememberLaunchedEffect
where you can attach a
Unit
key (so it ignores recomposition) and can you then attach some listener to be invoked when they view gets destroyed? Then it should be possible
The scope seems to get cancelled when not in the UI tree
But it's only in the effect block, and I need to use it outside of that block