Presentation
sinew-presentation and sinew-viewmodel on Kotlin; sinew_presentation and sinew_riverpod on Flutter.
A page has state (what it renders) and effects (one-time reactions), and effects are never state. sinew-presentation holds the pattern with no framework; the other module adapts it to a view model or a notifier.
What it holds
StateEffectHandler: a page with one intent.setState,emitEffect, and a stream of each.EventActionHandler: a page with several intents. The page sends events, a puremapEventToActiondecides the work, andonActiondoes it.AppServiceandAppServiceHost: work that lives with the app or the signed-in session and has no screen (a purchase from the store, a pub/sub message, a push). See App services.- On Kotlin,
StateEffectViewModelandEventActionViewModelare the same on an AndroidXViewModel, which survives rotation. On Flutter,StateEffectNotifierandEventActionNotifierare mixins on a RiverpodNotifier.
A page with one intent
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Presentation.kt#counter
data class CounterState(val count: Int = 0)
/** A one-time reaction: never state, so a rebuild can't repeat it. */
data object ReachedTen
class CounterViewModel : StateEffectViewModel<CounterState, ReachedTen>(CounterState()) {
fun increment() {
setState { copy(count = count + 1) }
if (state.value.count == 10) emitEffect(ReachedTen)
}
}
For a page with several intents, see the event and action version in the quick start.
App services
Some work isn't tied to a page: a purchase can arrive from the store at any time, with or without a screen open. A page handler can't own that, because it lives only as long as its page. An app service is the second kind of driver. Like a page handler it calls use cases and folds their Result, but it lives for the app or for the signed-in session, and it has no UI.
A service has three steps. init catches up on what happened while the app wasn't running, once per start and before listen. listen subscribes to the sources. destroy stops and releases. Guard each step with the service layer, so a bug in one is reported with an SV code (here BIL-SV-INIT-GEN).
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Presentation.kt#app-service
/** Work with the app's lifetime and no UI: purchases arrive from the store at any time, with or without a screen. */
class BillingService(
private val store: PurchaseStore,
private val acknowledge: AcknowledgePurchase,
) : AppService(ServiceLifetime.App) {
/** Catches up first, before [listen] subscribes. A bug here is reported as `BIL-SV-INIT-GEN`. */
override suspend fun init() {
processResult("BIL", "INIT", ExceptionLayer.Service) {
store.unacknowledged().forEach { acknowledge(it) }
}
}
override fun listen(scope: CoroutineScope) {
scope.launch {
store.arrivals.collect { purchase ->
processResult("BIL", "LISTEN", ExceptionLayer.Service) { acknowledge(purchase) }
}
}
}
}
An AppServiceHost starts and stops the services. Build it once at app start; startApp runs init then listen for the app-scope services in list order, and the session owner calls startSession after sign-in and stopSession after sign-out.
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Presentation.kt#app-service-host
/** Built once at app start, with the scope that outlives every screen. */
fun appServices(store: PurchaseStore, acknowledge: AcknowledgePurchase) = AppServiceHost(
appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default),
services = listOf(BillingService(store, acknowledge)),
)
/** App services start at launch, session services at sign-in and stop at sign-out. */
class AppLifecycle(private val services: AppServiceHost) {
fun launched() = services.startApp()
fun signedIn() = services.startSession()
suspend fun signedOut() = services.stopSession()
}
On Flutter with Riverpod, sinew_riverpod_providers holds the host: override appServicesProvider with your services, and read appServiceHostProvider once at startup, which starts the app services. Disposing the container destroys them.
- Kotlin
- Dart
Kotlin has no provider layer. Build the host once where the app starts (Application.onCreate, the iOS app's init) and keep it in your dependency container, as the sample above does.
Good to know
mapEventToActionis pure: it reads state and returns the work, ornullfor none right now. Work that must not overlap is guarded there, by reading state.onActionisn't awaited byonEvent, so actions may run concurrently. A throw in it is a bug and reaches the platform's error handler.- Clearing a view model, or disposing a provider, closes the effects. Late work then changes nothing.
- On Flutter,
useOnOpenedsends "the page opened" once, after the first build, anduseEffectslistens to effects while the page is mounted. - A service guards its own steps. Whatever a step still throws never stops the other services, or that service from listening: it is reported as an unhandled error, like any bug.
- A service's use cases are idempotent, because stores and push providers deliver the same event again. Pages never call or wait on a service: they observe the repository it writes to.
stopSessioncancels a session service'sinitthat is still running on Kotlin. Dart can't cancel anawait, so there that service just never starts listening. A secondstartSessionwhile a session runs does nothing.