Is there a way to access the key set of `SnapshotS...
# compose
r
Is there a way to access the key set of
SnapshotStateMap
in such a way as to only subscribe to updates of the key set (i.e. something is added or removed), and not value changes? Even if I have a composable that only uses
SnapshotStateMap.keys
, it seems to subscribe to the whole map
For context, I have a function that produces a map of items to display, and the offset to display them at (this executes in
withFrameMillis
in a
LaunchedEffect
). Think particles. I then have a
SubcomposeLayout
that I want to subcompose and measure each item in the map, and display it at the given offset. I'm attempting to get it to only re-measure when the items to display change, not their positions.
z
Nope. Under the hood a SnapshotStateMap is just a single state object reference to a persistent map object.
You could store the positions inside whatever you put in the map so you don’t have to update the map itself when positions change.
r
That sounds like it would work, something like
Map<ID, MutableState<Offset>>
s
If you want measure/placement invalidated separately, I'd recommend to just have singular state responsible for that calculation, e.g. derived state that reads original values or just something that triggers invalidation from setter. Many states are not /that/ cheap 🙂
r
You're saying have something like a
Map<ID, Unit>
for measure and
Map<ID, Unit>
for placement?
s
So let's say you want to only trigger measure whenever particles change and only trigger placement whenever position change. What you can do is have:
Copy code
val ids = derivedStateOf { map.keys }
val offsets = derivedStateOf { map.values }
Then you only read
ids
in measure and
offsets
in placement.
also also, i doubt that
SubcomposeLayout
is the best way to display particles. You are creating a composition per each object, which might be more expensive than just recomposing layout normally.
r
Yeah, it's not quite particles or I'd just do this in the graphics layer
s
Ok, I am not sure about exact requirements 🙂
another tip is that if your objects change somewhat frequently and they are pretty similar in structure, don't forget to enable reuse you can configure number of objects to reuse with
SubcomposeSlotReusePolicy
, there's a default one that just keeps a fixed number of items in reuse pool
👍 1
👍🏾 1