Skip to content

Commit 3e63b9e

Browse files
committed
Simplify detach operation (clean up call stack)
1 parent 7b69546 commit 3e63b9e

File tree

7 files changed

+20
-20
lines changed

7 files changed

+20
-20
lines changed

jooby/src/main/java/io/jooby/Context.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,10 @@ public interface Context extends Registry {
697697
*
698698
* This operation integrates easily with third-party libraries like rxJava or others.
699699
*
700-
* @param action Application code.
700+
* @param next Application code.
701701
* @return This context.
702702
*/
703-
@Nonnull Context detach(@Nonnull Runnable action);
703+
@Nonnull Context detach(@Nonnull Route.Handler next) throws Exception;
704704

705705
/*
706706
* **********************************************************************************************

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ public ForwardingContext(@Nonnull Context context) {
301301
return this;
302302
}
303303

304-
@Override @Nonnull public Context detach(@Nonnull Runnable action) {
305-
context.detach(action);
304+
@Override @Nonnull public Context detach(@Nonnull Route.Handler next) throws Exception {
305+
context.detach(next);
306306
return this;
307307
}
308308

jooby/src/main/java/io/jooby/internal/handler/DetachHandler.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@ public DetachHandler(Route.Handler next) {
1717
this.next = next;
1818
}
1919

20-
@Nonnull @Override public Object apply(@Nonnull Context ctx) {
21-
return ctx.detach(() -> {
22-
try {
23-
next.apply(ctx);
24-
} catch (Throwable x) {
25-
ctx.sendError(x);
26-
}
27-
});
20+
@Nonnull @Override public Object apply(@Nonnull Context ctx) throws Exception {
21+
return ctx.detach(next);
2822
}
2923

3024
@Override public Route.Handler next() {

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ public Context dispatch(@Nonnull Executor executor, @Nonnull Runnable action) {
244244
return this;
245245
}
246246

247-
@Nonnull @Override public Context detach(@Nonnull Runnable action) {
247+
@Nonnull @Override public Context detach(@Nonnull Route.Handler next) throws Exception {
248248
ifStartAsync();
249-
action.run();
249+
next.apply(this);
250250
return this;
251251
}
252252

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
172172
return this;
173173
}
174174

175-
@Nonnull @Override public Context detach(@Nonnull Runnable action) {
176-
action.run();
175+
@Nonnull @Override public Context detach(@Nonnull Route.Handler next) throws Exception {
176+
next.apply(this);
177177
return this;
178178
}
179179

modules/jooby-test/src/main/java/io/jooby/MockContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ public MockContext dispatch(@Nonnull Executor executor, @Nonnull Runnable action
295295
return this;
296296
}
297297

298-
@Nonnull @Override public MockContext detach(@Nonnull Runnable action) {
299-
action.run();
298+
@Nonnull @Override public MockContext detach(@Nonnull Route.Handler next) throws Exception {
299+
next.apply(this);
300300
return this;
301301
}
302302

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ public UtowContext(HttpServerExchange exchange, Router router) {
204204
return this;
205205
}
206206

207-
@Nonnull @Override public Context detach(@Nonnull Runnable action) {
208-
exchange.dispatch(SameThreadExecutor.INSTANCE, action);
207+
@Nonnull @Override public Context detach(@Nonnull Route.Handler next) {
208+
exchange.dispatch(SameThreadExecutor.INSTANCE, () -> {
209+
try {
210+
next.apply(this);
211+
} catch (Throwable x) {
212+
sendError(x);
213+
}
214+
});
209215
return this;
210216
}
211217

0 commit comments

Comments
 (0)