@@ -4,38 +4,40 @@ import scala.collection.mutable
4
4
5
5
/** A context that allows to suspend waiting for asynchronous data sources
6
6
*/
7
- trait Async extends Async . Config :
7
+ trait Async :
8
8
9
9
/** Wait for completion of async source `src` and return the result */
10
10
def await [T ](src : Async .Source [T ]): T
11
11
12
- /** An Async of the same kind as this one, with a given cancellation group */
13
- def withGroup (group : Cancellable .Group ): Async
12
+ /** The configuration of this Async */
13
+ def config : Async .Config
14
+
15
+ /** An Async of the same kind as this one, with a new configuration as given */
16
+ def withConfig (config : Async .Config ): Async
14
17
15
18
object Async :
16
19
17
20
/** The underlying configuration of an async block */
18
- trait Config :
19
-
20
- /** The group of cancellableup to which nested futures belong */
21
- def group : Cancellable .Group
21
+ case class Config (scheduler : Scheduler , group : Cancellable .Group )
22
22
23
- /** The scheduler for runnables defined in this async computation */
24
- def scheduler : Scheduler
25
-
26
- object Config :
23
+ trait LowPrioConfig :
27
24
28
25
/** A toplevel async group with given scheduler and a synthetic root that
29
26
* ignores cancellation requests
30
27
*/
31
- given fromScheduler (using s : Scheduler ): Config with
32
- def group = Cancellable .Unlinked
33
- def scheduler = s
28
+ given fromScheduler (using s : Scheduler ): Config = Config (s, Cancellable .Unlinked )
29
+
30
+ end LowPrioConfig
31
+
32
+ object Config extends LowPrioConfig :
33
+
34
+ /** The async configuration stored in the given async capabaility */
35
+ given fromAsync (using async : Async ): Config = async.config
34
36
35
37
end Config
36
38
37
39
/** An implementation of Async that blocks the running thread when waiting */
38
- private class Blocking (val scheduler : Scheduler , val group : Cancellable . Group ) extends Async :
40
+ private class Blocking (using val config : Config ) extends Async :
39
41
40
42
def await [T ](src : Source [T ]): T =
41
43
src.poll().getOrElse:
@@ -49,15 +51,14 @@ object Async:
49
51
while result.isEmpty do wait()
50
52
result.get
51
53
52
- def withGroup ( group : Cancellable . Group ) = Blocking (scheduler, group )
54
+ def withConfig ( config : Config ) = Blocking (using config )
53
55
end Blocking
54
56
55
-
56
57
/** Execute asynchronous computation `body` on currently running thread.
57
58
* The thread will suspend when the computation waits.
58
59
*/
59
- def blocking [T ](body : Async ?=> T , scheduler : Scheduler = Scheduler ): T =
60
- body(using Blocking (scheduler, Cancellable . Unlinked ))
60
+ def blocking [T ](body : Async ?=> T )( using Scheduler ): T =
61
+ body(using Blocking ())
61
62
62
63
/** The currently executing Async context */
63
64
inline def current (using async : Async ): Async = async
@@ -67,7 +68,7 @@ object Async:
67
68
68
69
def group [T ](body : Async ?=> T )(using async : Async ): T =
69
70
val newGroup = Cancellable .Group ().link()
70
- body(using async.withGroup( newGroup))
71
+ body(using async.withConfig(async.config.copy(group = newGroup) ))
71
72
72
73
/** A function `T => Boolean` whose lineage is recorded by its implementing
73
74
* classes. The Listener function accepts values of type `T` and returns
0 commit comments