Skip to content

Fix sample code for ReactorContext #2682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class kotlinx/coroutines/reactor/ReactorContext : kotlin/coroutines
public fun <init> (Lreactor/util/context/Context;)V
public fun <init> (Lreactor/util/context/ContextView;)V
public final fun getContext ()Lreactor/util/context/Context;
public fun toString ()Ljava/lang/String;
}

public final class kotlinx/coroutines/reactor/ReactorContext$Key : kotlin/coroutines/CoroutineContext$Key {
Expand Down
8 changes: 5 additions & 3 deletions reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ import reactor.util.context.*
* ```
* val flux = myDatabaseService.getUsers()
* .contextWrite { ctx -> println(ctx); ctx }
* flux.await() // Will print "null"
* flux.awaitFirst() // Will print "null"
*
* // Now add ReactorContext
* withContext(Context.of("answer", "42").asCoroutineContext()) {
* flux.await() // Will print "Context{'key'='value'}"
* flux.awaitFirst() // Will print "Context{'key'='value'}"
* }
* ```
*
* #### Propagating subscriber's Context to ReactorContext:
* ```
* val flow = flow {
* println("Reactor context in Flow: " + coroutineContext[ReactorContext])
* println("Reactor context in Flow: " + currentCoroutineContext()[ReactorContext])
* }
* // No context
* flow.asFlux()
Expand All @@ -55,6 +55,8 @@ public class ReactorContext(public val context: Context) : AbstractCoroutineCont
public constructor(contextView: ContextView): this(Context.of(contextView))

public companion object Key : CoroutineContext.Key<ReactorContext>

override fun toString(): String = context.toString()
}

/**
Expand Down