-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #424 from JetBrains/threading-usov
Threading usov
- Loading branch information
Showing
24 changed files
with
566 additions
and
113 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
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
13 changes: 13 additions & 0 deletions
13
...ore/src/main/kotlin/com/jetbrains/rd/util/threading/coroutines/ISchedulerCoroutineUtil.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,13 @@ | ||
package com.jetbrains.rd.util.threading.coroutines | ||
|
||
import com.jetbrains.rd.util.reactive.IScheduler | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
private class SchedulerCoroutineDispatcher(private val scheduler: IScheduler, private val allowInlining: Boolean) : CoroutineDispatcher() { | ||
override fun dispatch(context: CoroutineContext, block: Runnable) = scheduler.queue { block.run() } | ||
override fun isDispatchNeeded(context: CoroutineContext) = !allowInlining || !scheduler.isActive | ||
} | ||
|
||
val IScheduler.asCoroutineDispatcher get() = (this as? CoroutineDispatcher) ?: asCoroutineDispatcher(false) | ||
fun IScheduler.asCoroutineDispatcher(allowInlining: Boolean): CoroutineDispatcher = SchedulerCoroutineDispatcher(this, allowInlining) |
64 changes: 64 additions & 0 deletions
64
...d-core/src/main/kotlin/com/jetbrains/rd/util/threading/coroutines/ISourceCoroutineUtil.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,64 @@ | ||
package com.jetbrains.rd.util.threading.coroutines | ||
|
||
import com.jetbrains.rd.util.lifetime.Lifetime | ||
import com.jetbrains.rd.util.reactive.IScheduler | ||
import com.jetbrains.rd.util.reactive.ISource | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.Deferred | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
fun <T> ISource<T>.nextValueAsync( | ||
lifetime: Lifetime, | ||
condition: (T) -> Boolean = { true } | ||
): Deferred<T> = CompletableDeferred<T>().also { d -> | ||
val nestedDef = lifetime.createNested().apply { | ||
synchronizeWith(d) | ||
} | ||
|
||
advise(nestedDef.lifetime) { | ||
if (condition(it)) { | ||
d.complete(it) | ||
} | ||
} | ||
} | ||
|
||
fun <T : Any> ISource<T?>.nextNotNullValueAsync(lifetime: Lifetime): Deferred<T> = CompletableDeferred<T>().also { d -> | ||
val nestedDef = lifetime.createNested().apply { | ||
synchronizeWith(d) | ||
} | ||
|
||
advise(nestedDef.lifetime) { | ||
if (it != null) { | ||
d.complete(it) | ||
} | ||
} | ||
} | ||
|
||
suspend fun <T> ISource<T>.nextValue( | ||
condition: (T) -> Boolean = { true } | ||
): T = Lifetime.using { // unsubscribe if coroutine was cancelled | ||
nextValueAsync(it, condition).await() | ||
} | ||
|
||
fun ISource<Boolean>.nextTrueValueAsync(lifetime: Lifetime) = nextValueAsync(lifetime) { it } | ||
fun ISource<Boolean>.nextFalseValueAsync(lifetime: Lifetime) = nextValueAsync(lifetime) { !it } | ||
|
||
suspend fun ISource<Boolean>.nextTrueValue() = nextValue { it } | ||
suspend fun ISource<Boolean>.nextFalseValue() = nextValue { !it } | ||
|
||
suspend fun <T : Any> ISource<T?>.nextNotNullValue(): T = | ||
Lifetime.using { // unsubscribe if coroutine was cancelled | ||
nextNotNullValueAsync(it).await() | ||
} | ||
|
||
fun<T> ISource<T>.adviseSuspend(lifetime: Lifetime, scheduler: IScheduler, handler: suspend (T) -> Unit) { | ||
adviseSuspend(lifetime, scheduler.asCoroutineDispatcher(allowInlining = true), handler) | ||
} | ||
|
||
fun<T> ISource<T>.adviseSuspend(lifetime: Lifetime, context: CoroutineContext, handler: suspend (T) -> Unit) { | ||
advise(lifetime) { | ||
lifetime.launch(context) { | ||
handler(it) | ||
} | ||
} | ||
} |
Oops, something went wrong.