https://kotlinlang.org logo
#compose
Title
# compose
k

Kazemihabib1996

05/13/2020, 2:26 PM
When should we use
staticAmbientOf
instead of
ambientOf
? what's the benefit of using
staticAmbientOf
that
RippleTheme
for example uses that instead of
ambientOf
?
a

Adam Powell

05/13/2020, 2:37 PM
staticAmbientOf
has lower overhead but if it ever changes in place, we will invalidate and recompose the entire subhierarchy below the
Providers
where it changed, since we don't track precise location of reads for static ambients.
In effect, "dynamic" ambients are just a static ambient that holds a
State<T>
that holds the "real" value of the ambient
so in the case of dynamic ambients, that
State<T>
instance from the
Providers
in the composition stays constant and never changes, only the observable value changes when you provide a new value for the ambient key.
k

Kazemihabib1996

05/13/2020, 2:47 PM
@Adam Powell Thank you So you used
staticAmbientOf
for
RippleTheme
as in most applications developers set that one time and won't change it later?
c

Chuck Jazdzewski [G]

05/13/2020, 4:39 PM
That is correct. Use
staticAmbientOf
for ambients that are unlikely to ever change or if they do change they likely invalidate the entire tree anyway. Here, change is defined as changing the value provided at the point it is provided. If a static ambient is re-provided, it is not counted as being changed and will not invalidate the tree below it, the tree below just sees a different value than the tree above.
👍 1