@@ -44,10 +44,11 @@ private fun <T> reactorPublish(
44
44
scope : CoroutineScope ,
45
45
context : CoroutineContext = EmptyCoroutineContext ,
46
46
@BuilderInference block : suspend ProducerScope <T >.() -> Unit
47
- ): Publisher <T > = Publisher { subscriber ->
48
- // specification requires NPE on null subscriber
49
- if (subscriber == null ) throw NullPointerException (" Subscriber cannot be null" )
50
- require(subscriber is CoreSubscriber ) { " Subscriber is not an instance of CoreSubscriber, context can not be extracted." }
47
+ ): Publisher <T > = Publisher onSubscribe@{ subscriber: Subscriber <in T >? ->
48
+ if (subscriber !is CoreSubscriber ) {
49
+ subscriber.reject(IllegalArgumentException (" Subscriber is not an instance of CoreSubscriber, context can not be extracted." ))
50
+ return @onSubscribe
51
+ }
51
52
val currentContext = subscriber.currentContext()
52
53
val reactorContext = (context[ReactorContext ]?.context?.putAll(currentContext) ? : currentContext).asCoroutineContext()
53
54
val newContext = scope.newCoroutineContext(context + reactorContext)
@@ -67,6 +68,23 @@ private val REACTOR_HANDLER: (Throwable, CoroutineContext) -> Unit = { cause, ct
67
68
}
68
69
}
69
70
71
+ /* * The proper way to reject the subscriber, according to
72
+ * [the reactive spec](https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#1.9)
73
+ */
74
+ private fun <T > Subscriber<T>?.reject (t : Throwable ) {
75
+ if (this == null )
76
+ throw NullPointerException (" The subscriber can not be null" )
77
+ onSubscribe(object : Subscription {
78
+ override fun request (n : Long ) {
79
+ // intentionally left blank
80
+ }
81
+ override fun cancel () {
82
+ // intentionally left blank
83
+ }
84
+ })
85
+ onError(t)
86
+ }
87
+
70
88
@Deprecated(
71
89
message = " CoroutineScope.flux is deprecated in favour of top-level flux" ,
72
90
level = DeprecationLevel .HIDDEN ,
0 commit comments