Add support for Deferred to ReactiveAdapterRegistry
See gh-19975
This commit is contained in:
@@ -40,8 +40,8 @@ import org.springframework.util.ReflectionUtils;
|
||||
* {@code Observable}, and others.
|
||||
*
|
||||
* <p>By default, depending on classpath availability, adapters are registered
|
||||
* for Reactor, RxJava 1, RxJava 2 types, {@link CompletableFuture}, and Java 9+
|
||||
* Flow.Publisher.
|
||||
* for Reactor, RxJava 1, RxJava 2 types, {@link CompletableFuture}, Java 9+
|
||||
* {@code Flow.Publisher} and Kotlin Coroutines {@code Deferred}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
@@ -90,6 +90,11 @@ public class ReactiveAdapterRegistry {
|
||||
}
|
||||
// If not present, do nothing for the time being...
|
||||
// We can fall back on "reactive-streams-flow-bridge" (once released)
|
||||
|
||||
// Coroutines
|
||||
if (ClassUtils.isPresent("kotlinx.coroutines.Deferred", classLoader)) {
|
||||
CoroutinesRegistrarKt.registerAdapter(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core
|
||||
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.reactive.awaitFirstOrNull
|
||||
import kotlinx.coroutines.reactor.mono
|
||||
import reactor.core.publisher.toMono
|
||||
|
||||
/**
|
||||
* Register Reactive adapters for Coroutines types.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
internal fun registerAdapter(registry: ReactiveAdapterRegistry) {
|
||||
registry.registerReactiveType(
|
||||
ReactiveTypeDescriptor.singleOptionalValue(Deferred::class.java) { GlobalScope.async {} },
|
||||
{ source -> GlobalScope.mono { (source as Deferred<*>).await() }},
|
||||
{ source -> GlobalScope.async { source.toMono().awaitFirstOrNull() } }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user