arrow-fx-coroutines / arrow.fx.coroutines.stream / Scope
class ~~Scope~~
Deprecated: Stream is deprecated in favor of Flow
Implementation of Scope for the internal stream interpreter.
Represents a period of stream execution in which resources are acquired and released. A scope has a state, consisting of resources (with associated finalizers) acquired in this scope and child scopes spawned from this scope.
=== Scope lifetime ===
When stream interpretation starts, one root
scope is created. Scopes are then created and closed based on the
stream structure. Every time a Pull
is converted to a Stream
, a scope is created.
For example, s.chunks
is defined with s.repeatPull
which in turn is defined with Pull.loop(...).stream
.
In this case, a single scope is created as a result of the call to .stream
.
Scopes may also be opened and closed manually with Stream#scope
. For the stream s.scope
, a scope
is opened before evaluation of s
and closed once s
finishes evaluation.
=== Scope organization ===
Scopes are organized in tree structure, with each scope having at max one parent (a root scope has no parent) with 0 or more child scopes.
Every time a new scope is created, it inherits parent from the current scope and adds itself as a child of that parent.
During the interpretation of nondeterministic streams (i.e. merge), there may be multiple scopes attached to a single parent and these scopes may be created and closed in a nondeterministic order.
A child scope never outlives its parent scope. I.e., when a parent scope is closed for whatever reason, the child scopes are closed too.
=== Resources ===
The primary role of a scope is tracking resource allocation and release. The stream interpreter guarantees that resources allocated in a scope are always released when the scope closes.
=== Resource allocation ===
Resources are allocated when the interpreter interprets the Acquire
element, which is typically constructed
via Stream.bracket
. See arrow.fx.coroutines.Resource docs for more information.
id
- Unique identification of the scope
parent
- If empty indicates root scope. If non-empty, indicates parent of this scope.
interruptible
- If defined, allows this scope to interrupt any of its operation. Interruption
is performed using the supplied context.
Normally the interruption awaits next step in Pull to be evaluated, with exception
of Eval, that when interruption is enabled on scope will be wrapped in race,
that eventually allows interruption while eval is evaluating.
Lease | 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 |
interrupt | Interrupts evaluation of the current scope. Only scopes previously indicated with Stream.interruptScope may be interrupted. For other scopes this will fail.suspend fun interrupt(cause: Either< Throwable , Unit >): Unit |
lease | Leases the resources of this scope until the returned lease is cancelled.suspend fun lease(): Lease? |
toString | fun toString(): String |
Do you like Arrow?
✖