Skip to content

Commit 30f3389

Browse files
committed
Split concurrency library into separate files
1 parent 7b83493 commit 30f3389

File tree

5 files changed

+247
-204
lines changed

5 files changed

+247
-204
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package concurrent
2+
3+
/** A context that allows to suspend waiting for asynchronous data sources */
4+
trait Async:
5+
6+
/** Wait for completion of async source `src` and return the result */
7+
def await[T](src: Async.Source[T]): T
8+
9+
/** Wait for completion of the first of the sources `src1`, `src2`
10+
* @return `Left(r1)` if `src1` completed first with `r1`
11+
* `Right(r2)` if `src2` completed first with `r2`
12+
*/
13+
def awaitEither[T1, T2](src1: Async.Source[T1], src2: Async.Source[T2]): Either[T1, T2]
14+
15+
/** The cancellable runner underlying this async computation. */
16+
def runner: Cancellable
17+
18+
/** The scheduler for runnables defined in this async computation */
19+
def scheduler: Scheduler
20+
21+
object Async:
22+
23+
/** The currently executing Async context */
24+
inline def current(using async: Async): Async = async
25+
26+
/** An asynchronous data source. Sources can be persistent or ephemeral.
27+
* A persistent source will always return the same data to calls to `poll`
28+
* and pass the same data to calls of `handle`. An ephemeral source might pass new
29+
* data in every call. An example of a persistent source is `Future`. An
30+
* example of an ephemeral source is `Channel`.
31+
*/
32+
trait Source[+T]:
33+
thisSource =>
34+
35+
/** Poll whether data is available
36+
* @return The data or None in an option. Depending on the nature of the
37+
* source, data might be returned only once in a poll. E.g. if
38+
* the source is a channel, a Some result might skip to the next
39+
*/
40+
def poll: Option[T]
41+
42+
/** When data is available, pass it to function `k`.
43+
*/
44+
def handleWith(k: T => Unit): Unit
45+
46+
def map[U](f: T => U): Source[U] = new Source:
47+
def poll = thisSource.poll.map(f)
48+
def handleWith(k: U => Unit): Unit = thisSource.handleWith(f.andThen(k))
49+
50+
end Source
51+
52+
end Async
53+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package concurrent
2+
3+
/** A trait for cancellable entiries that can be grouped */
4+
trait Cancellable:
5+
6+
def cancel(): Unit
7+
8+
/** Add a given child to this Cancellable, so that the child will be cancelled
9+
* when the Cancellable itself is cancelled.
10+
*/
11+
def addChild(child: Cancellable): Unit
12+
13+
def isCancelled: Boolean
14+
15+
end Cancellable

0 commit comments

Comments
 (0)