leonard-palm / compose-state-events

A new way to implement One-Time-UI-Events (former SingleLiveEvent) in a Compose world.
Apache License 2.0
178 stars 14 forks source link

Mark StateEvent(WithContent) as Stable/Immutable #4

Closed mikef-dk closed 1 year ago

mikef-dk commented 1 year ago

Hi,

I'm wondering what your thoughts are about marking the StateEvent / StateEventWithContent and its implementation as either Stable or Immutable. I think both are applicable (though Immutable should be more fitting) and would enable the Composables where those types are used to be skippable.

Any thoughts on this?

As reference: Jetpack Compose Stability Explained

leonard-palm commented 1 year ago

Hi @mikef-dk, thank you for your thoughts ragarding performance improvements.

StateEvent and StateEventWithContent are both composable functions. The annotation @Immutable is targeting classes only. So composables can't be marked as immutable this way. The compose compiler consideres a composable to be skippable if all passed parameters are immutable and did not change.

I went ahead and marked StateEvent and StateEventWithContent explicitly as immutable. Furthermore I found the @NonRestartableComposable annotiation which seems fitting for all the event side effect.

After these 2 optimisations, the third parameter action: suspend (T) -> Unit is the only one that stays unstable. I think this is pretty much all the library can optimize at this point.

When using the event side effects, one could try remembering the action lambda according to this article. If this prevents recompositions strongly depends on the individual implementations of your action lambda(s).

Compose compiler reports:

stable class Triggered {
}
stable class Consumed {
}
stable class StateEventWithContentTriggered {
  runtime val content: T
}
stable class StateEventWithContentConsumed {
}
fun EventEffect(
  stable event: StateEvent
  stable onConsumed: Function0<Unit>
  unstable action: SuspendFunction0<Unit>
)
fun EventEffect(
  stable event: StateEventWithContent<T>
  stable onConsumed: Function0<Unit>
  unstable action: SuspendFunction1<T, Unit>
)
fun NavigationEventEffect(
  stable event: StateEvent
  stable onConsumed: Function0<Unit>
  unstable action: SuspendFunction0<Unit>
)
fun NavigationEventEffect(
  stable event: StateEventWithContent<T>
  stable onConsumed: Function0<Unit>
  unstable action: SuspendFunction1<T, Unit>
)