Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/api/EventsHub.kt
T
HomerandClaude Fable 5.2 1d3154cefa
CI / check (push) Canceled after 0s
Review: a save says only what changed, the picker and the @ list are bounded, followers survive a finger
- Schedule form: `runs_on`, `model` and the watched command are sent only
  where they differ from what was read (whereToSend, modelToSend,
  watchToSend). The type is not choosable until an edited schedule was
  read. A Chat schedule's model is shown when it is made Agentic. Modes by
  the computer's `max_mode`. The Agentic form has the watched command and
  its timeout; Agentic to Chat takes the command off and says so.
- Picker: each path once and at most 500 entries, the device's words cut
  and ellipsised, Back closes after a question that got no listing, what
  was typed while the device answered stays, a listing's own sentence shown.
- @: at most 50 entries; a device path with control characters or over 512
  characters is not offered; asked again when the computer or folder
  changes; attaching no longer ends another upload's "uploading".
- Followers: a scroll a finger interrupts no longer ends the collector
  (yielding), and nothing is scrolled while a scroll is under way. Only a
  message this app sent counts as Sent.
- /events: one shared connection for the drawer and the Scheduled list
  (EventsHub); a route that closes without a frame backs off; a slow list
  read no longer overwrites a newer `reports` count.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 01:04:24 +00:00

47 lines
2.0 KiB
Kotlin

package eu.llembas.android.api
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.flow.shareIn
import java.util.concurrent.ConcurrentHashMap
/**
* One connection to a person's events route (`GET /events`) for everything that watches it --
* the drawer's Reports badge, the Scheduled list -- instead of one each. It is open while
* somebody watches and closed when nobody does.
*
* Every frame of the route is a whole state (`schedules`, `reports`), which the server sends at
* once to a new connection. A watcher that joins a connection already open missed that, so it
* is first told the last frame of each kind this connection carried.
*
* A route that refuses (an older server) ends quietly: its watchers hear nothing, as they did
* when each asked for itself.
*/
class EventsHub(private val scope: CoroutineScope) {
private class Line(val key: String, val frames: SharedFlow<Frame>, val latest: MutableMap<String, Frame>)
private var line: Line? = null
/** The frames of the route [key] names (an instance's id), opened by [open] when nobody watches it yet. */
@Synchronized
fun frames(key: String, open: () -> Flow<Frame>): Flow<Frame> {
val now = line?.takeIf { it.key == key } ?: run {
val latest = ConcurrentHashMap<String, Frame>()
val shared = open()
// A connection opened anew is told everything again: what the last one said is stale.
.onStart { latest.clear() }
.onEach { latest[it.event] = it }
.catch { }
.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 0)
Line(key, shared, latest).also { line = it }
}
return now.frames.onSubscription { now.latest.values.toList().forEach { emit(it) } }
}
}