GitHub

LayerRef

API reference for LayerRef.

Make one with LayerRef.make(layer) inside a scope.

Attributes

AttributeTypeDescription
rc_ref= rc_ref

Functions

__init__

__init__(rc_ref) -> None
ParameterTypeDescription
rc_refRcRef[Context[I], E]
def __init__(self, rc_ref: RcRef[Context[I], E]) -> None:
    self.rc_ref = rc_ref
    self._layer: Layer[I, E] = effect_context(rc_ref.get())

make

make(layer, *, idle_time_to_live=None, preload=False, invalidation_schedule=None) -> Effect[LayerRef[Any, Any], Any, Any]

A ref building layer (with the services in scope now, sharing what enclosing builds built) when first used. Released when the current scope closes.

ParameterTypeDescription
layerLayer[Any, Any, Any]
idle_time_to_liveDuration | None = None
preloadbool = False
invalidation_scheduleSchedule[object, object, object, Any] | None = None

Returns Effect[LayerRef[Any, Any], Any, Any]

@staticmethod
def make(
    layer: Layer[Any, Any, Any],
    *,
    idle_time_to_live: Duration | None = None,
    preload: bool = False,
    invalidation_schedule: Schedule[object, object, object, Any] | None = None,
) -> Effect[LayerRef[Any, Any], Any, Any]:
    """A ref building ``layer`` (with the services in scope now, sharing
    what enclosing builds built) when first used. Released when the
    current scope closes."""
    require_layer(layer, "layer")

    def body():
        context: Context = yield from _WithFiber(lambda fiber: _Succeed(fiber._context))
        memo_map = fork_or_create(context)
        build: Effect[Context, Any, Any] = Scope().flat_map(
            lambda scope: layer.build_with_memo_map(memo_map, scope)
        )
        rc_ref = yield from RcRef.make(build, idle_time_to_live=idle_time_to_live)
        ref: LayerRef[Any, Any] = LayerRef(rc_ref)
        if preload:
            yield from ref.refresh()
        if invalidation_schedule is not None:
            on_recur = ref.refresh() if preload else ref.invalidate()
            yield from on_recur.ignore_cause().schedule(invalidation_schedule).fork_scoped()
        return ref

    return _Gen(body)

get

get() -> Layer[I, E]

A layer giving the cached services, building them if needed.

Returns Layer[I, E]

def get(self) -> Layer[I, E]:
    """A layer giving the cached services, building them if needed."""
    return self._layer

context_effect

context_effect() -> Effect[Context[I], E, Scope]

The cached services, built if needed and held until the current scope closes.

Returns Effect[Context[I], E, Scope]

def context_effect(self) -> Effect[Context[I], E, Scope]:
    """The cached services, built if needed and held until the current
    scope closes."""
    return self.rc_ref.get()

invalidate

invalidate() -> Effect[None]

Forget the build, so the next use rebuilds the layer.

Returns Effect[None]

def invalidate(self) -> Effect[None]:
    """Forget the build, so the next use rebuilds the layer."""
    return self.rc_ref.invalidate()

refresh

refresh() -> Effect[None, E]

Forget the build and build the layer again now.

Returns Effect[None, E]

def refresh(self) -> Effect[None, E]:
    """Forget the build and build the layer again now."""
    return self.invalidate().and_then(scoped(self.rc_ref.get())).as_void()