|
21 | 21 | import java.util.Objects;
|
22 | 22 |
|
23 | 23 | import kotlin.Unit;
|
| 24 | +import kotlin.coroutines.CoroutineContext; |
24 | 25 | import kotlin.jvm.JvmClassMappingKt;
|
25 | 26 | import kotlin.reflect.KClass;
|
26 | 27 | import kotlin.reflect.KClassifier;
|
@@ -66,17 +67,39 @@ public static <T> Deferred<T> monoToDeferred(Mono<T> source) {
|
66 | 67 | (scope, continuation) -> MonoKt.awaitSingleOrNull(source, continuation));
|
67 | 68 | }
|
68 | 69 |
|
| 70 | + /** |
| 71 | + * Invoke a suspending function and converts it to {@link Mono} or |
| 72 | + * {@link Flux}. Uses an {@linkplain Dispatchers#getUnconfined() unconfined} |
| 73 | + * dispatcher. |
| 74 | + * @param method the suspending function to invoke |
| 75 | + * @param target the target to invoke {@code method} on |
| 76 | + * @param args the function arguments |
| 77 | + * @return the method invocation result as reactive stream |
| 78 | + */ |
| 79 | + public static Publisher<?> invokeSuspendingFunction(Method method, Object target, |
| 80 | + Object... args) { |
| 81 | + return invokeSuspendingFunction(Dispatchers.getUnconfined(), method, target, args); |
| 82 | + } |
| 83 | + |
69 | 84 | /**
|
70 | 85 | * Invoke a suspending function and converts it to {@link Mono} or
|
71 | 86 | * {@link Flux}.
|
| 87 | + * @param context the coroutine context to use |
| 88 | + * @param method the suspending function to invoke |
| 89 | + * @param target the target to invoke {@code method} on |
| 90 | + * @param args the function arguments |
| 91 | + * @return the method invocation result as reactive stream |
| 92 | + * @since 6.0 |
72 | 93 | */
|
73 | 94 | @SuppressWarnings("deprecation")
|
74 |
| - public static Publisher<?> invokeSuspendingFunction(Method method, Object target, Object... args) { |
| 95 | + public static Publisher<?> invokeSuspendingFunction(CoroutineContext context, Method method, Object target, |
| 96 | + Object... args) { |
| 97 | + |
75 | 98 | KFunction<?> function = Objects.requireNonNull(ReflectJvmMapping.getKotlinFunction(method));
|
76 | 99 | if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
|
77 | 100 | KCallablesJvm.setAccessible(function, true);
|
78 | 101 | }
|
79 |
| - Mono<Object> mono = MonoKt.mono(Dispatchers.getUnconfined(), (scope, continuation) -> |
| 102 | + Mono<Object> mono = MonoKt.mono(context, (scope, continuation) -> |
80 | 103 | KCallables.callSuspend(function, getSuspendedFunctionArgs(target, args), continuation))
|
81 | 104 | .filter(result -> !Objects.equals(result, Unit.INSTANCE))
|
82 | 105 | .onErrorMap(InvocationTargetException.class, InvocationTargetException::getTargetException);
|
|
0 commit comments