Permalink
Browse files

Move Lease to Scope's companion object

  • Loading branch information...
SystemFw committed Jan 10, 2018
1 parent b1c4b30 commit fb3d074a82c89cf7c5ecf93fab42949f581ac37e

This file was deleted.

Oops, something went wrong.
@@ -25,7 +25,7 @@ abstract class Scope[F[_]] {
* When the lease is returned, all resources available at the time `lease` was called have been
* successfully leased.
*/
def lease: F[Option[Lease[F]]]
def lease: F[Option[Scope.Lease[F]]]
/**
* Interrupts evaluation of the current scope. Only scopes previously indicated wih Stream.interruptScope may be interrupted.
@@ -43,3 +43,21 @@ abstract class Scope[F[_]] {
def interrupt(cause: Either[Throwable, Unit]): F[Unit]
}
object Scope {
/**
* Represents one or more resources that were leased from a scope, causing their
* lifetimes to be extended until `cancel` is invoked on this lease.
*/
abstract class Lease[F[_]] {
/**
* Cancels the lease of all resources tracked by this lease.
*
* This may run finalizers on some of the resources (depending on the state of their owning scopes).
* If one or more finalizers fail, the returned action completes with a `Left(t)`, providing the failure.
*/
def cancel: F[Either[Throwable, Unit]]
}
}
@@ -4,7 +4,7 @@ import scala.annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
import cats.data.NonEmptyList
import fs2.{Catenable, CompositeFailure, Lease, Scope}
import fs2.{Catenable, CompositeFailure, Scope}
import fs2.async.{Promise, Ref}
import cats.effect.{Effect, Sync}
import fs2.internal.CompileScope.InterruptContext
@@ -252,7 +252,7 @@ private[fs2] final class CompileScope[F[_], O] private (
}
// See docs on [[Scope#lease]]
def lease: F[Option[Lease[F]]] = {
def lease: F[Option[Scope.Lease[F]]] = {
val T = Catenable.instance
F.flatMap(state.get) { s =>
if (!s.open) F.pure(None)
@@ -268,9 +268,9 @@ private[fs2] final class CompileScope[F[_], O] private (
val allLeases = leased.collect {
case Some(resourceLease) => resourceLease
}
val lease = new Lease[F] {
val lease = new Scope.Lease[F] {
def cancel: F[Either[Throwable, Unit]] =
traverseError[Lease[F]](allLeases, _.cancel)
traverseError[Scope.Lease[F]](allLeases, _.cancel)
}
Some(lease)
}
@@ -3,7 +3,7 @@ package fs2.internal
import java.util.concurrent.atomic.AtomicReference
import cats.effect.Sync
import fs2.Lease
import fs2.Scope
import fs2.async.Ref
/**
@@ -78,7 +78,7 @@ private[internal] sealed abstract class Resource[F[_]] {
* Yields to `Some(lease)`, if this resource was successfully leased, and scope must bind `lease.concel` it when not needed anymore.
* or to `None` when this resource cannot be leased because resource is already released.
*/
def lease: F[Option[Lease[F]]]
def lease: F[Option[Scope.Lease[F]]]
}
private[internal] object Resource {
@@ -131,14 +131,14 @@ private[internal] object Resource {
}
}
def lease: F[Option[Lease[F]]] =
def lease: F[Option[Scope.Lease[F]]] =
F.map(state.modify { s =>
if (!s.open) s
else s.copy(leases = s.leases + 1)
}) { c =>
if (!c.now.open) None
else {
val lease = new Lease[F] {
val lease = new Scope.Lease[F] {
def cancel: F[Either[Throwable, Unit]] =
F.flatMap(state.modify { s =>
s.copy(leases = s.leases - 1)

0 comments on commit fb3d074

Please sign in to comment.