-
Notifications
You must be signed in to change notification settings - Fork 29
/
Task.scala
168 lines (153 loc) · 4.95 KB
/
Task.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.thoughtworks.dsl
package domains
import com.thoughtworks.dsl.domains.Continuation, Continuation.!!
import com.thoughtworks.dsl.keywords.Pure
import com.thoughtworks.dsl.keywords.Suspend
import com.thoughtworks.dsl.keywords.Shift
import com.thoughtworks.dsl.macros.Reset.Default.*
import scala.collection._
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future, Promise, SyncVar}
import scala.util.control.{NonFatal, TailCalls}
import scala.util.{Failure, Success, Try}
import scala.util.control.TailCalls.TailRec
/** The asynchronous task that supports exception handling, resource management,
* and is stack-safe.
*
* @template
* @example
* A [[Task]] can be created from `for`-comprehension, where
* [[keywords.Each]] can be used together to asynchronously iterate
* collections.
*
* For example, the above `concatenateRemoteData` downloads and concatenates
* data from multiple URLs.
*
* {{{
* import com.thoughtworks.dsl._
* import com.thoughtworks.dsl.keywords._
* import com.thoughtworks.dsl.domains.Task
* import java.net.URL
* def concatenateRemoteData(urls: List[URL], downloader: URL => Task[Vector[Byte]]) = Each.ToView {
* for {
* url <- Each(urls)
* data <- Shift(downloader(url))
* byte <- Each(data)
* } yield byte
* }.to[Task]
* }}}
*
* A [[Task]] can be also created from [[Task.apply]]
*
* {{{
* def mockDownloader(url: URL) = Task {
* "mock data\n".getBytes.toVector
* }
* }}}
*
* A [[Task]] can be then converted to [[scala.concurrent.Future]] via
* [[Task.toFuture]], in order to integrate into other frameworks.
*
* In this example, it's a `Future[Assertion]` required by
* [[org.scalatest.freespec.AsyncFreeSpec]].
*
* {{{
* val mockUrls = List(new URL("http://example.com/file1"), new URL("http://example.com/file2"))
*
* import org.scalatest.Assertion
* def assertion: Task[Assertion] = Task {
* new String((!Shift(concatenateRemoteData(mockUrls, mockDownloader))).toArray) should be("mock data\nmock data\n")
* }
*
* Task.toFuture(assertion)
* }}}
*/
type Task[+A] = Task.TaskDomain !! A
/** @author
* 杨博 (Yang Bo)
*/
object Task extends TaskPlatformSpecificFunctions {
type TaskDomain = TailRec[Unit] !! Throwable
@inline
def now[A](a: A): Task[A] = _(a)
@inline
def delay[A](f: () => A): Task[A] = _(f())
inline def apply[A](inline a: A): Task[A] = { handler =>
reset {
// !Pure ensures stack safety
handler(!Pure(a))
}
}
/** Returns a task that does nothing but let the succeeding tasks run on
* `executionContext`
*
* @example
* All the code after a `!switchExecutionContext` should be executed on
* `executionContext`
* {{{
* import com.thoughtworks.dsl.macros.Reset.Default.*
* import com.thoughtworks.dsl.domains.Task
* import org.scalatest.Assertion
* import scala.concurrent.ExecutionContext
* import com.thoughtworks.dsl.keywords.Shift
* def myTask: Task[Assertion] = Task {
* val originalThread = Thread.currentThread
* !Shift(Task.switchExecutionContext(ExecutionContext.global))
* Thread.currentThread should not be originalThread
* }
*
* Task.toFuture(myTask)
*
* }}}
*/
@inline
def switchExecutionContext(executionContext: ExecutionContext): Task[Unit] = {
continue => failureHandler =>
executionContext.execute(new Runnable {
@inline
private def stackSafeRun(): TailRec[Unit] = {
val protectedContinuation =
try {
continue(())
} catch {
case NonFatal(e) =>
return failureHandler(e)
}
protectedContinuation(failureHandler)
}
def run(): Unit = stackSafeRun().result
})
TailCalls.done(())
}
@inline
private def newBuilder[A, C](implicit
factory: Factory[A, C]
): Builder[A, C] = {
factory.newBuilder
}
def onComplete[A](task: Task[A])(continue: Try[A] => Unit) = {
Continuation
.toTryContinuation(task) { result =>
TailCalls.done(continue(result))
}
.result
}
/** Converts a [[Task]] to a [[scala.concurrent.Future]].
*
* @see
* [[keywords.Await]] for converting a [[scala.concurrent.Future]] to a
* [[Task]].
*/
def toFuture[A](task: Task[A]): Future[A] = {
val promise = Promise[A]()
Continuation
.toTryContinuation(task) { tryResult =>
promise.complete(tryResult)
TailCalls.done(())
}
.result
promise.future
}
}