-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
107 changed files
with
5,248 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
ballast-api/src/commonMain/kotlin/com/copperleaf/ballast/internal/RestartableJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.copperleaf.ballast.internal | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.consumeAsFlow | ||
import kotlinx.coroutines.launch | ||
|
||
public interface RestartableJob<T> : Job { | ||
public fun restart(context: T) | ||
} | ||
|
||
public fun <T> CoroutineScope.restartableJob( | ||
block: suspend CoroutineScope.(T) -> Unit, | ||
): RestartableJob<T> { | ||
val channel = Channel<T>(Channel.CONFLATED) | ||
val job = launch { | ||
channel.consumeAsFlow().collectLatest { block(it) } | ||
} | ||
return object : RestartableJob<T>, Job by job { | ||
override fun restart(context: T) { | ||
channel.trySend(context) | ||
} | ||
} | ||
} |
Oops, something went wrong.