Yokan / rebuild
Course 1 8624561

ProfileDownloadBindings.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/download/ProfileDownloadBindings.kt

implementation62 lines
1package eu.friedcutlet.yokan.services.download23import eu.friedcutlet.yokan.domain.ProfileId4import eu.friedcutlet.yokan.services.runJoined5import eu.friedcutlet.yokan.services.scheduling.ProfileWorkResult6import eu.friedcutlet.yokan.util.PlatformLock7import kotlinx.coroutines.CoroutineScope8import kotlinx.coroutines.Job9import kotlinx.coroutines.SupervisorJob10import kotlinx.coroutines.currentCoroutineContext11import kotlinx.coroutines.ensureActive12import kotlinx.coroutines.launch1314/** Process-local capabilities. Notification actions never bootstrap or reopen a profile. */15internal class ProfileDownloadBindings {16    private val lock = PlatformLock()17    private val bindings = mutableMapOf<ProfileId, Binding>()1819    fun install(id: ProfileId, downloads: DownloadService, ownerScope: CoroutineScope, hideContent: () -> Boolean = { false }): Binding {20        val parent = requireNotNull(ownerScope.coroutineContext[Job])21        require(parent.isActive) { "Download owner is retired" }22        val binding = Binding(id, downloads, CoroutineScope(ownerScope.coroutineContext + SupervisorJob(parent)), hideContent)23        val previous = lock.withLock { bindings.put(id, binding) }24        previous?.retire()25        binding.job.invokeOnCompletion {26            lock.withLock { if (bindings[id] === binding) bindings.remove(id) }27        }28        return binding29    }3031    fun resolve(owner: String?, token: String?): Binding? {32        if (owner == null || token == null) return null33        val id = try { ProfileId(owner) } catch (_: IllegalArgumentException) { return null }34        return lock.withLock { bindings[id]?.takeIf { it.token == token && it.isActive } }35    }3637    class Binding internal constructor(38        val id: ProfileId,39        val downloads: DownloadService,40        private val scope: CoroutineScope,41        /** Notification privacy of the owning profile (Security → hide notification content). */42        val hideContent: () -> Boolean = { false },43    ) {44        // Distinguishes two sessions for the same profile; it is not a credential or a persistent ID.45        val token: String = ProfileId.random().value46        val actionIdentity: String get() = "yokan://downloads/${id.value}/$token"47        internal val job = requireNotNull(scope.coroutineContext[Job])48        val isActive: Boolean get() = job.isActive4950        fun cancelAll() { scope.launch { downloads.cancelAll() } }51        fun retire() { job.cancel() }5253        suspend fun run(action: suspend (DownloadService) -> Unit): ProfileWorkResult<Unit> {54            currentCoroutineContext().ensureActive()55            if (!isActive) return ProfileWorkResult.Skipped56            scope.runJoined { action(downloads) }57            return ProfileWorkResult.Completed(Unit)58        }59    }6061    companion object { const val TokenKey = "yokan.download_session" }62}63

Search chapters, reference and source filenames.