git.iko.soy/opencode-android
on 0bc1eda help sign in
opencode-android/app/src/main/java/soy/iko/opencode/di/OpencodeConnection.kt
31 lines 1.1 kB history raw
1package soy.iko.opencode.di
3import soy.iko.opencode.data.model.ServerProfile
4import soy.iko.opencode.data.network.EventStreamClient
5import soy.iko.opencode.data.network.HttpClientFactory
6import soy.iko.opencode.data.network.OpencodeApiClient
7import soy.iko.opencode.data.repo.SessionRepository
8import kotlinx.coroutines.CoroutineScope
9import kotlinx.coroutines.Dispatchers
10import kotlinx.coroutines.SupervisorJob
11import kotlinx.coroutines.cancel
13/**
14 * An active connection to one opencode server. Bundles the Ktor client, REST API,
15 * SSE event stream, and the reducing repository, all sharing a connection-scoped
16 * coroutine scope that is cancelled on [close].
17 */
18class OpencodeConnection(val profile: ServerProfile) {
20 private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
22 private val client = HttpClientFactory.create(profile)
23 val api = OpencodeApiClient(client)
24 val events = EventStreamClient(client, scope)
25 val repository = SessionRepository(api, events)
27 fun close() {
28 scope.cancel()
29 client.close()
30 }