GitHub

Resource

API reference for Resource.

Make one with Resource.manual or Resource.auto inside a scope.

Functions

__init__

__init__(ref, acquire) -> None
ParameterTypeDescription
refScopedRef[Exit[A, E]]
acquireEffect[A, E]
def __init__(self, ref: ScopedRef[Exit[A, E]], acquire: Effect[A, E]) -> None:
    self._ref = ref
    self._acquire = acquire

manual

manual(acquire) -> Effect[Resource[T, X], Never, Scope | R]

A resource acquired now (with the services in scope now) and again on each refresh.

ParameterTypeDescription
acquireEffect[T, X, R]

Returns Effect[Resource[T, X], Never, Scope | R]

@staticmethod
def manual[T, X = Never, R = Never](
    acquire: Effect[T, X, R],
) -> Effect[Resource[T, X], Never, Scope | R]:
    """A resource acquired now (with the services in scope now) and
    again on each ``refresh``."""
    require_effect(acquire, "acquire")

    def body(fiber: Fiber[Any, Any]) -> Effect[Resource[T, X], Never, Scope]:
        captured = fiber._context
        provided: Effect[T, X] = _Locally(acquire, captured.merge)
        return ScopedRef.from_acquire(provided.exit()).map(lambda ref: Resource(ref, provided))

    return _WithFiber(body)

auto

auto(acquire, policy) -> Effect[Resource[T, X], Never, Scope | RR]

manual, refreshed each time policy recurs (first after its first delay) until the current scope closes. Failed refreshes are ignored.

ParameterTypeDescription
acquireEffect[T, X, RR]
policySchedule[object, object, object, RR]

Returns Effect[Resource[T, X], Never, Scope | RR]

@staticmethod
def auto[T, X = Never, RR = Never](
    acquire: Effect[T, X, RR], policy: Schedule[object, object, object, RR]
) -> Effect[Resource[T, X], Never, Scope | RR]:
    """``manual``, refreshed each time ``policy`` recurs (first after its
    first delay) until the current scope closes. Failed refreshes are
    ignored."""
    require_effect(acquire, "acquire")
    return Resource.manual(acquire).tap(
        lambda resource: resource.refresh().ignore_cause().schedule(policy).fork_scoped()
    )

get

get() -> Effect[A, E]

The cached value, or the cached failure.

Returns Effect[A, E]

def get(self) -> Effect[A, E]:
    """The cached value, or the cached failure."""
    return self._ref.get().flat_map(_FromExit)

refresh

refresh() -> Effect[None, E]

Acquire anew and swap the value in. A failure keeps the cached value.

Returns Effect[None, E]

def refresh(self) -> Effect[None, E]:
    """Acquire anew and swap the value in. A failure keeps the cached
    value."""
    return self._ref.set(self._acquire.map(Success))