Skip to content

Commit 2becf2b

Browse files
committed
Add context-function method to Action
1 parent c170c90 commit 2becf2b

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
3+
*/
4+
5+
package play.api.mvc
6+
7+
trait ActionBuilderMethods[+R[_], B] { self: ActionBuilder[R, B] =>
8+
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
3+
*/
4+
5+
package play.api.mvc
6+
7+
import scala.concurrent.Future
8+
9+
trait ActionBuilderMethods[+R[_], B] { self: ActionBuilder[R, B] =>
10+
11+
/**
12+
* Constructs an `Action` that returns a future of a result, with default content.
13+
*
14+
* @param block the action code
15+
* @return an action
16+
*/
17+
final def handle(block: R[B] ?=> Future[Result]): Action[B] =
18+
async(request => block(using request))
19+
20+
/**
21+
* Constructs an `Action` with the given [[BodyParser]] that returns a future of a result.
22+
*
23+
* @param block the action code
24+
* @return an action
25+
*/
26+
final def handle[A](bodyParser: BodyParser[A])(block: R[A] ?=> Future[Result]): Action[A] =
27+
async(bodyParser)(request => block(using request))
28+
}

core/play/src/main/scala/play/api/mvc/Action.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ trait ActionFunction[-R[_], +P[_]] {
314314
/**
315315
* Provides helpers for creating [[Action]] values.
316316
*/
317-
trait ActionBuilder[+R[_], B] extends ActionFunction[Request, R] {
317+
trait ActionBuilder[+R[_], B] extends ActionFunction[Request, R] with ActionBuilderMethods[R, B] {
318318
self =>
319319

320320
/**

0 commit comments

Comments
 (0)