Hotswap

sealed trait Hotswap[F <: ([_$1] =>> Any), R]
Supports treating a linear sequence of resources as a single resource.
A Hotswap[F, R] instance is created as a Resource and hence, has
a lifetime that is scoped by the Resource. After creation, a Resource[F, R]
can be swapped in to the Hotswap by calling swap. The acquired resource
is returned and is finalized when the Hotswap is finalized or upon the next
call to swap, whichever occurs first.
For example, the sequence of three resources r1, r2, r3 are shown in the
following diagram:
{{{

        
----- swap(r1) ---- swap(r2) ---- swap(r3) ----X
| | | | |
Creation | | | |
r1 acquired | | |
r2 acquired | |
r1 released r3 acquired |
r2 released |
r3 released
}}}
This class is particularly useful when working with pulls that cycle through
resources -- e.g., writing bytes to files, rotating files every N bytes or M seconds.
Without Hotswap, such pulls leak resources -- on each file rotation, a file handle
or at least an internal resource reference accumulates. With Hotswap, the Hotswap
instance is the only registered resource and each file is swapped in to the Hotswap.
Usage typically looks something like:
{{{
Stream.resource(Hotswap(mkResource)).flatMap { case (hotswap, r) =>
// Use r, call hotswap.swap(mkResource) as necessary
}
}}}
See fs2.io.file.writeRotate for an example of usage.
Companion
object
class Object
trait Matchable
class Any

Value members

Methods

def swap(next: Resource[F, R]): F[R]
Allocates a new resource, closes the last one if present, and
returns the newly allocated R.
If there are no further calls to swap, the resource created by
the last call will be finalized when the lifetime of
this Hotswap (which is itself tracked by Resource) is over.
Since swap closes the old resource immediately, you need to
ensure that no code is using the old R when swap is called.
Failing to do so is likely to result in an error on the
consumer side. In any case, no resources will be leaked by
swap.
If you try to call swap after the lifetime of this Hotswap is
over, swap will fail, but it will ensure all resources are
closed, and never leak any.
def clear: F[Unit]
Runs the finalizer of the current resource, if any, and restores
this Hotswap to its initial state.
Like swap, you need to ensure that no code is using the old R when
clear is called. Similarly, calling clear after the lifetime of this
Hotswap results in an error.