Skip to content

Commit

Permalink
ReactJS: Add a lambda helper to BeanPool.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehearn committed Oct 7, 2024
1 parent 08e0987 commit 74132e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ class ReactViewsRenderer<PROPS> implements ViewsRenderer<PROPS, HttpRequest<?>>,
* or introspectable object), returns hydratable HTML that can be booted on the client using
* the React libraries.
*
* @param viewName The function or class name of the React component to use as the root. It should return an html root tag.
* @param viewName The function or class name of the React component to use as the root. It should return an HTML root tag.
* @param props If non-null, will be exposed to the given component as React props.
* @param request The HTTP request object.
*/
@Override
public @NonNull Writable render(@NonNull String viewName, @Nullable PROPS props, @Nullable HttpRequest<?> request) {
return writer -> {
try (BeanPool.Handle<ReactJSContext> contextHandle = beanPool.checkOut()) {
render(viewName, props, writer, contextHandle.get(), request);
try {
beanPool.useContext(handle -> {
render(viewName, props, writer, handle.get(), request);
return null;
});
} catch (BeanInstantiationException e) {
throw e;
} catch (Exception e) {
Expand All @@ -82,9 +85,7 @@ class ReactViewsRenderer<PROPS> implements ViewsRenderer<PROPS, HttpRequest<?>>,

@Override
public boolean exists(@NonNull String viewName) {
try (var contextHandle = beanPool.checkOut()) {
return contextHandle.get().moduleHasMember(viewName);
}
return beanPool.useContext(handle -> handle.get().moduleHasMember(viewName));
}

private void render(String componentName, PROPS props, Writer writer, ReactJSContext context, @Nullable HttpRequest<?> request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.views.react.util;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.util.functional.ThrowingFunction;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -115,6 +116,29 @@ public synchronized void checkIn(Handle<T> handle) {
}
}

/**
* Runs the block with a handle checked out, checking it back in again at the end even if an
* exception is thrown.
*
* <p>
* This is implemented equivalently to:
* <pre>
* try (Handle<T> handle = checkOut()) {
* return block.apply(handle);
* }
* </pre>
*
* @param block The lambda that will be executed with the checked out handle.
* @param <R> Return type of the block.
* @param <E> What type of exception the block can throw.
* @return the same object returned by the block.
*/
public <R, E extends Throwable> R useContext(ThrowingFunction<Handle<T>, R, E> block) throws E {
try (Handle<T> handle = checkOut()) {
return block.apply(handle);
}
}

/**
* Empties the pool. Beans currently checked out with {@link #checkOut()} will not be re-added
* to the pool when {@link #checkIn(Handle)} is called, and may be closed if they are
Expand Down

0 comments on commit 74132e4

Please sign in to comment.