-
Notifications
You must be signed in to change notification settings - Fork 511
/
Async.scala
137 lines (107 loc) · 3.93 KB
/
Async.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
/*
* Copyright 2017 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats
package effect
import simulacrum._
import cats.data.{EitherT, OptionT, StateT, WriterT}
import scala.annotation.implicitNotFound
import scala.concurrent.ExecutionContext
import scala.util.{Either, Right}
/**
* A monad that can describe asynchronous or synchronous computations that
* produce exactly one result.
*/
@typeclass
@implicitNotFound("""Cannot find implicit value for Async[${F}].
Building this implicit value might depend on having an implicit
s.c.ExecutionContext in scope, a Strategy or some equivalent type.""")
trait Async[F[_]] extends Sync[F] with LiftIO[F] {
/**
* Creates an `F[A]` instance from a provided function
* that will have a callback injected for signaling the
* final result of an asynchronous process.
*
* @param k is a function that should be called with a
* callback for signaling the result once it is ready
*/
def async[A](k: (Either[Throwable, A] => Unit) => Unit): F[A]
/**
* @see [[IO#shift]]
*/
def shift(implicit ec: ExecutionContext): F[Unit] = {
async { (cb: Either[Throwable, Unit] => Unit) =>
ec.execute(new Runnable {
def run() = cb(Right(()))
})
}
}
override def liftIO[A](ioa: IO[A]): F[A] = {
// Able to provide default with `IO#to`, given this `Async[F]`
ioa.to[F](this)
}
}
private[effect] trait AsyncInstances {
implicit def catsEitherTAsync[F[_]: Async, L]: Async[EitherT[F, L, ?]] =
new EitherTAsync[F, L] { def F = Async[F] }
implicit def catsOptionTAsync[F[_]: Async]: Async[OptionT[F, ?]] =
new OptionTAsync[F] { def F = Async[F] }
implicit def catsStateTAsync[F[_]: Async, S]: Async[StateT[F, S, ?]] =
new StateTAsync[F, S] { def F = Async[F] }
implicit def catsWriterTAsync[F[_]: Async, L: Monoid]: Async[WriterT[F, L, ?]] =
new WriterTAsync[F, L] { def F = Async[F]; def L = Monoid[L] }
private[effect] trait EitherTAsync[F[_], L]
extends Async[EitherT[F, L, ?]]
with Sync.EitherTSync[F, L]
with LiftIO.EitherTLiftIO[F, L] {
override protected def F: Async[F]
private implicit def _F = F
protected def FF = F
def async[A](k: (Either[Throwable, A] => Unit) => Unit): EitherT[F, L, A] =
EitherT.liftF(F.async(k))
}
private[effect] trait OptionTAsync[F[_]]
extends Async[OptionT[F, ?]]
with Sync.OptionTSync[F]
with LiftIO.OptionTLiftIO[F] {
override protected def F: Async[F]
private implicit def _F = F
protected def FF = F
def async[A](k: (Either[Throwable, A] => Unit) => Unit): OptionT[F, A] =
OptionT.liftF(F.async(k))
}
private[effect] trait StateTAsync[F[_], S]
extends Async[StateT[F, S, ?]]
with Sync.StateTSync[F, S]
with LiftIO.StateTLiftIO[F, S] {
override protected def F: Async[F]
private implicit def _F = F
protected def FA = F
def async[A](k: (Either[Throwable, A] => Unit) => Unit): StateT[F, S, A] =
StateT.liftF(F.async(k))
}
private[effect] trait WriterTAsync[F[_], L]
extends Async[WriterT[F, L, ?]]
with Sync.WriterTSync[F, L]
with LiftIO.WriterTLiftIO[F, L] {
override protected def F: Async[F]
private implicit def _F = F
protected def FA = F
private implicit def _L = L
def async[A](k: (Either[Throwable, A] => Unit) => Unit): WriterT[F, L, A] =
WriterT.liftF(F.async(k))
}
}
object Async extends AsyncInstances