Yokan / rebuild
Course 1 8624561

ProfileBackgroundWork.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/app/profiles/ProfileBackgroundWork.kt

implementation42 lines
1package eu.friedcutlet.yokan.app.profiles23import kotlinx.coroutines.CancellationException4import kotlinx.coroutines.currentCoroutineContext5import kotlinx.coroutines.ensureActive6import eu.friedcutlet.yokan.domain.ProfileId7import eu.friedcutlet.yokan.services.scheduling.ProfileWorkResult89internal interface ProfileWorkSessions {10    suspend fun <T> run(id: ProfileId, action: suspend (ProfileSession) -> T): ProfileWorkResult<T>11}1213/**14 * Checks fresh metadata before opening any database. No lookup uses lastActive or falls back from15 * an explicit invalid/unavailable owner. Production uses the coordinator's storage lease; retired owners never recreate their data.16 */17internal class ProfileBackgroundWork(18    private val registry: ProfileRegistry,19    private val supported: (ProfileId) -> Boolean,20    private val sessions: ProfileWorkSessions,21) {22    suspend fun <T> run(23        serializedOwner: String?,24        allowLegacyDefault: Boolean = false,25        action: suspend (ProfileSession) -> T,26    ): ProfileWorkResult<T> {27        currentCoroutineContext().ensureActive()28        val id = if (serializedOwner == null) {29            if (!allowLegacyDefault) return ProfileWorkResult.Skipped30            ProfileId.Default31        } else {32            try { ProfileId(serializedOwner) }33            catch (_: IllegalArgumentException) { return ProfileWorkResult.Skipped }34        }35        val snapshot = try { registry.refresh() }36        catch (error: CancellationException) { throw error }37        catch (_: Exception) { return ProfileWorkResult.Retry }38        currentCoroutineContext().ensureActive()39        if (snapshot.available(id) == null || !supported(id)) return ProfileWorkResult.Skipped40        return sessions.run(id, action)41    }42}43

Search chapters, reference and source filenames.