Shivam Sethi
03/30/2021, 4:35 AMShivam Sethi
03/30/2021, 8:17 AMknthmn
03/30/2021, 1:55 PMisLiveText
in a state to prevent recomposition.knthmn
03/30/2021, 1:57 PMZach Klippenstein (he/him) [MOD]
03/30/2021, 4:23 PMisLiveText
isn’t a property here, it’s just a parameter, so it’s definitely not a delegate and won’t use the snapshot state read tracking system that MutableState
uses. Whenever ScanScreen
is called, if any of its parameters are different than the last call, its body will be re-executed. As part of that reexecution, and composables to which you’ve passed lambdas which capture that property will also be recomposed, since the lambda objects need to be recreated to capture the new value.
As for recognizedText
, the reason that ScanPreview
gets called whenever it changes is trickier. If Box
were a normal, non-inlined function, it would still get called because ScanPreview
is called from the recompose scope that is delimited by the lambda you’re passing to Box
. recognizedText
is read in that same recompose scope, in order to pass its value to AnalyzedText
. So when the state changes, that recompose scope (ie that lambda) is re-executed. However, Box
is actually an inline function, which means it doesn’t get its own recompose scope, which means that any state read inside `Box`’s lambda will actually cause all of ScanScreen
to recompose anyway.
It’s important to note that compose doesn’t try to re-execute only parts of functions. If a state changes that is read in a given recompose scope, that entire recompose scope is recomposed. Also important to remember that values passed to functions are calculated in the bytecode of the calling function. If you call println("hello" + "world")
, that string concatenation is effectively performed in the line before println
, in the calling function. That means that if you pass the value of a snapshot state object to a function, that state read is recorded in the calling recompose scope, not the one of the function the value is being passed to.Shivam Sethi
03/31/2021, 2:00 AMZach Klippenstein (he/him) [MOD]
03/31/2021, 6:08 PMShivam Sethi
03/31/2021, 8:22 PM