Skip to content
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

Refactor ReactivePropertyAccessor by wrapping existing PropertyAccessor #6686

Merged
merged 1 commit into from
Sep 23, 2024
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
@@ -0,0 +1,62 @@
package run.halo.app.infra.utils;

import java.time.Duration;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* Utility class for reactive.
*
* @author johnniang
* @since 2.20.0
*/
public enum ReactiveUtils {
;

private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);

/**
* Resolve reactive value by blocking operation.
*
* @param value the normal value or reactive value
* @return the resolved value
*/
@Nullable
public static Object blockReactiveValue(@Nullable Object value) {
return blockReactiveValue(value, DEFAULT_TIMEOUT);
}

/**
* Resolve reactive value by blocking operation.
*
* @param value the normal value or reactive value
* @param timeout the timeout of blocking operation
* @return the resolved value
*/
@Nullable
public static Object blockReactiveValue(@Nullable Object value, @NonNull Duration timeout) {
if (value == null) {
return null;
}
Class<?> clazz = value.getClass();
if (Mono.class.isAssignableFrom(clazz)) {
return ((Mono<?>) value).blockOptional(timeout).orElse(null);
}
if (Flux.class.isAssignableFrom(clazz)) {
return ((Flux<?>) value).collectList().block(timeout);
}
return value;
}

/**
* Check if the class is a reactive type.
*
* @param clazz the class to check
* @return true if the class is a reactive type, false otherwise
*/
public static boolean isReactiveType(@NonNull Class<?> clazz) {
return Mono.class.isAssignableFrom(clazz) || Flux.class.isAssignableFrom(clazz);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package run.halo.app.theme;

import java.util.Optional;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator;
import org.thymeleaf.standard.expression.IStandardVariableExpression;
import org.thymeleaf.standard.expression.IStandardVariableExpressionEvaluator;
import org.thymeleaf.standard.expression.StandardExpressionExecutionContext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.infra.utils.ReactiveUtils;

/**
* Reactive SPEL variable expression evaluator.
Expand All @@ -17,28 +17,25 @@
public class ReactiveSpelVariableExpressionEvaluator
implements IStandardVariableExpressionEvaluator {

private final SPELVariableExpressionEvaluator delegate =
SPELVariableExpressionEvaluator.INSTANCE;
private final IStandardVariableExpressionEvaluator delegate;

public static final ReactiveSpelVariableExpressionEvaluator INSTANCE =
new ReactiveSpelVariableExpressionEvaluator();

public ReactiveSpelVariableExpressionEvaluator(IStandardVariableExpressionEvaluator delegate) {
this.delegate = delegate;
}

public ReactiveSpelVariableExpressionEvaluator() {
this(SPELVariableExpressionEvaluator.INSTANCE);
}

@Override
public Object evaluate(IExpressionContext context, IStandardVariableExpression expression,
StandardExpressionExecutionContext expContext) {
Object returnValue = delegate.evaluate(context, expression, expContext);
if (returnValue == null) {
return null;
}

Class<?> clazz = returnValue.getClass();
// Note that: 3 instanceof Foo -> syntax error
if (Mono.class.isAssignableFrom(clazz)) {
return ((Mono<?>) returnValue).block();
}
if (Flux.class.isAssignableFrom(clazz)) {
return ((Flux<?>) returnValue).collectList().block();
}
return returnValue;
var returnValue = delegate.evaluate(context, expression, expContext);
return Optional.ofNullable(returnValue)
.map(ReactiveUtils::blockReactiveValue)
.orElse(null);
}
}
Loading
Loading