Skip to content

Commit 99f5ac9

Browse files
authored
Add fromZIONow variant of fromZIO (#465)
1 parent bd5f5b8 commit 99f5ac9

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

zio-query/shared/src/main/scala/zio/query/ZQuery.scala

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ final class ZQuery[-R, +E, +A] private (private val step: ZIO[R, Nothing, Result
291291
* Maps the specified effectual function over the result of this query.
292292
*/
293293
final def mapZIO[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B])(implicit trace: Trace): ZQuery[R1, E1, B] =
294-
flatMap(a => ZQuery.fromZIO(f(a)))
294+
flatMap(a => ZQuery.fromZIONow(f(a)))
295295

296296
/**
297297
* Converts this query to one that returns `Some` if data sources return
@@ -378,7 +378,7 @@ final class ZQuery[-R, +E, +A] private (private val step: ZIO[R, Nothing, Result
378378
case Continue.Effect(query) =>
379379
ZIO.succeed(Result.blocked(blockedRequests, Continue.effect(race(query, fiber))))
380380
case Continue.Get(io) =>
381-
ZIO.succeed(Result.blocked(blockedRequests, Continue.effect(race(ZQuery.fromZIO(io), fiber))))
381+
ZIO.succeed(Result.blocked(blockedRequests, Continue.effect(race(ZQuery.fromZIONow(io), fiber))))
382382
}
383383
case Result.Done(value) => fiber.interrupt *> ZIO.succeed(Result.done(value))
384384
case Result.Fail(cause) => fiber.join.map(_.mapErrorCause(_ && cause))
@@ -534,9 +534,9 @@ final class ZQuery[-R, +E, +A] private (private val step: ZIO[R, Nothing, Result
534534
ZQuery.suspend {
535535
val summary = summary0
536536
for {
537-
start <- ZQuery.fromZIO(summary)
537+
start <- ZQuery.fromZIONow(summary)
538538
value <- self
539-
end <- ZQuery.fromZIO(summary)
539+
end <- ZQuery.fromZIONow(summary)
540540
} yield (f(start, end), value)
541541
}
542542

@@ -966,7 +966,7 @@ object ZQuery {
966966
* Accesses the whole environment of the query.
967967
*/
968968
def environment[R](implicit trace: Trace): ZQuery[R, Nothing, ZEnvironment[R]] =
969-
ZQuery.fromZIO(ZIO.environment)
969+
ZQuery(ZIO.environmentWith[R](Result.done))
970970

971971
/**
972972
* Accesses the environment of the effect.
@@ -1160,7 +1160,7 @@ object ZQuery {
11601160
Continue.get(io)
11611161
} else {
11621162
val query = ZQuery.collectAllBatched(effects).flatMap { effects =>
1163-
ZQuery.fromZIO(ZIO.collectAll(gets).map { gets =>
1163+
ZQuery.fromZIONow(ZIO.collectAll(gets).map { gets =>
11641164
val array = Array.ofDim[AnyRef](index)
11651165
val effectsIterator = effects.iterator
11661166
val effectIndicesIterator = effectIndices.iterator
@@ -1378,11 +1378,24 @@ object ZQuery {
13781378
def fromZIO[R, E, A](effect: => ZIO[R, E, A])(implicit trace: Trace): ZQuery[R, E, A] =
13791379
ZQuery(ZIO.suspendSucceed(effect).foldCause(Result.fail, Result.done))
13801380

1381+
/**
1382+
* Constructs a query from an effect. Unlike [[fromZIO]], this method does not
1383+
* suspend the creation of the effect which can lead to improved performance
1384+
* in some cases, but it should only be used when the creation of the effect
1385+
* is side effect-free.
1386+
*
1387+
* Note that this is method is meant mostly for internal use, but it's made
1388+
* public so that library authors can make use of this optimization. Most
1389+
* users should use [[fromZIO]] instead.
1390+
*/
1391+
def fromZIONow[R, E, A](effect: ZIO[R, E, A])(implicit trace: Trace): ZQuery[R, E, A] =
1392+
ZQuery(effect.foldCause(Result.fail, Result.done))
1393+
13811394
/**
13821395
* Constructs a query that never completes.
13831396
*/
13841397
def never(implicit trace: Trace): ZQuery[Any, Nothing, Nothing] =
1385-
ZQuery.fromZIO(ZIO.never)
1398+
ZQuery.fromZIONow(ZIO.never)
13861399

13871400
/**
13881401
* Constructs a query that succeds with the empty value.
@@ -1418,7 +1431,7 @@ object ZQuery {
14181431
* Accesses the whole environment of the query.
14191432
*/
14201433
def service[R: Tag](implicit trace: Trace): ZQuery[R, Nothing, R] =
1421-
ZQuery.fromZIO(ZIO.service)
1434+
ZQuery(ZIO.serviceWith[R](Result.done))
14221435

14231436
/**
14241437
* Accesses the environment of the effect.
@@ -1526,7 +1539,7 @@ object ZQuery {
15261539
ZIO.succeed(Result.blocked(blockedRequests, Continue.effect(race(query, fiber))))
15271540
case Continue.Get(io) =>
15281541
ZIO.succeed(
1529-
Result.blocked(blockedRequests, Continue.effect(race(ZQuery.fromZIO(io), fiber)))
1542+
Result.blocked(blockedRequests, Continue.effect(race(ZQuery.fromZIONow(io), fiber)))
15301543
)
15311544
}
15321545
case Result.Done(value) => rightFiber.interrupt *> ZIO.succeed(Result.done(value))
@@ -1537,7 +1550,7 @@ object ZQuery {
15371550
)
15381551
}
15391552

1540-
ZQuery.fromZIO(ZIO.sleep(duration).interruptible.as(b()).fork).flatMap(fiber => race(self.map(f), fiber))
1553+
ZQuery.fromZIONow(ZIO.sleep(duration).interruptible.as(b()).fork).flatMap(fiber => race(self.map(f), fiber))
15411554
}
15421555
}
15431556

@@ -1634,19 +1647,19 @@ object ZQuery {
16341647
.suspend(use(a))
16351648
.foldCauseQuery(
16361649
cause =>
1637-
ZQuery.fromZIO {
1650+
ZQuery.fromZIONow {
16381651
ZIO
16391652
.suspendSucceed(release(a, Exit.failCause(cause)))
16401653
.whenZIO(ref.getAndSet(false))
16411654
.mapErrorCause(cause ++ _) *>
16421655
ZIO.refailCause(cause)
16431656
},
16441657
b =>
1645-
ZQuery.fromZIO {
1658+
ZQuery.fromZIONow {
16461659
ZIO
16471660
.suspendSucceed(release(a, Exit.succeed(b)))
1648-
.whenZIO(ref.getAndSet(false)) *>
1649-
ZIO.succeed(b)
1661+
.whenZIO(ref.getAndSet(false))
1662+
.as(b)
16501663
}
16511664
)
16521665
}

zio-query/shared/src/main/scala/zio/query/internal/Continue.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private[query] sealed trait Continue[-R, +E, +A] { self =>
5353
)(implicit trace: Trace): Continue[R1, E1, B] =
5454
self match {
5555
case Effect(query) => effect(query.foldCauseQuery(failure, success))
56-
case Get(io) => effect(ZQuery.fromZIO(io).foldCauseQuery(failure, success))
56+
case Get(io) => effect(ZQuery.fromZIONow(io).foldCauseQuery(failure, success))
5757
}
5858

5959
/**
@@ -100,7 +100,7 @@ private[query] sealed trait Continue[-R, +E, +A] { self =>
100100
)(implicit trace: Trace): Continue[R1, E1, B] =
101101
self match {
102102
case Effect(query) => effect(query.flatMap(f))
103-
case Get(io) => effect(ZQuery.fromZIO(io).flatMap(f))
103+
case Get(io) => effect(ZQuery.fromZIONow(io).flatMap(f))
104104
}
105105

106106
/**
@@ -123,8 +123,8 @@ private[query] sealed trait Continue[-R, +E, +A] { self =>
123123
)(f: (A, B) => C)(implicit trace: Trace): Continue[R1, E1, C] =
124124
(self, that) match {
125125
case (Effect(l), Effect(r)) => effect(l.zipWith(r)(f))
126-
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIO(r))(f))
127-
case (Get(l), Effect(r)) => effect(ZQuery.fromZIO(l).zipWith(r)(f))
126+
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIONow(r))(f))
127+
case (Get(l), Effect(r)) => effect(ZQuery.fromZIONow(l).zipWith(r)(f))
128128
case (Get(l), Get(r)) => get(l.zipWith(r)(f))
129129
}
130130

@@ -137,8 +137,8 @@ private[query] sealed trait Continue[-R, +E, +A] { self =>
137137
)(f: (A, B) => C)(implicit trace: Trace): Continue[R1, E1, C] =
138138
(self, that) match {
139139
case (Effect(l), Effect(r)) => effect(l.zipWithPar(r)(f))
140-
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIO(r))(f))
141-
case (Get(l), Effect(r)) => effect(ZQuery.fromZIO(l).zipWith(r)(f))
140+
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIONow(r))(f))
141+
case (Get(l), Effect(r)) => effect(ZQuery.fromZIONow(l).zipWith(r)(f))
142142
case (Get(l), Get(r)) => get(l.zipWith(r)(f))
143143
}
144144

@@ -151,8 +151,8 @@ private[query] sealed trait Continue[-R, +E, +A] { self =>
151151
)(f: (A, B) => C)(implicit trace: Trace): Continue[R1, E1, C] =
152152
(self, that) match {
153153
case (Effect(l), Effect(r)) => effect(l.zipWithBatched(r)(f))
154-
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIO(r))(f))
155-
case (Get(l), Effect(r)) => effect(ZQuery.fromZIO(l).zipWith(r)(f))
154+
case (Effect(l), Get(r)) => effect(l.zipWith(ZQuery.fromZIONow(r))(f))
155+
case (Get(l), Effect(r)) => effect(ZQuery.fromZIONow(l).zipWith(r)(f))
156156
case (Get(l), Get(r)) => get(l.zipWith(r)(f))
157157
}
158158
}
@@ -195,7 +195,7 @@ private[query] object Continue {
195195
as.zip(queries.map(_._2)).foreach { case (a, i) =>
196196
array(i) = a.asInstanceOf[AnyRef]
197197
}
198-
ZQuery.fromZIO(ZIO.collectAll(ios.map(_._1))).map { as =>
198+
ZQuery.fromZIONow(ZIO.collectAll(ios.map(_._1))).map { as =>
199199
as.zip(ios.map(_._2)).foreach { case (a, i) =>
200200
array(i) = a.asInstanceOf[AnyRef]
201201
}

0 commit comments

Comments
 (0)