variable / @typed/fx/Fx

provideServiceEffect

Acquires one service with an Effect before running the Fx.

Package version
2.0.0-beta.13
Category
Providing services
Since
1.0.0

Follow one execution

Acquires one service with an Effect before running the Fx.

Fx timelineprovideServiceEffect runs one service Effect before forwarding the source valuesprovideServiceEffect
OperatorprovideServiceEffect(Config, makeConfig)
Read this diagram

Follow each lane from left to right. Events stacked vertically share a tick; the green cursor marks the current time across every lane.

  • A value
    The text inside the pill is the emitted value.
  • Work starts
    The raised chevron starts an inner run (^ in the source).
  • The run returns
    The vertical bar ends this lane’s run.
  • A cause is delivered
    The exclamation mark belongs to this lane.
  • Work is interrupted
    The cross marks cancellation of this run.
  • Current time
    The line and diamond move together across all lanes.
  • Happening now
    A highlighted event is at the current tick.
  • Still ahead
    Muted, dashed values have not happened yet.
  • Time continues
    The lane’s arrow is not a return marker. An empty stretch can be quiet work that is still running.

Illustrated ticks start at 0. At 1×, one illustrated tick takes one second; captions specify real durations when timing matters. A cause or interruption belongs to its lane, and other work may continue. Scroll horizontally to inspect the rest of a long timeline.

The service Effect runs once per subscription while provide builds its Layer. A failure prevents the source from starting. On success, the service is available for the whole source run and its identifier is removed from R; acquisition errors and every requirement in R2 are added. In particular, an Effect.acquireRelease service Effect still leaves Scope.Scope in the returned Fx requirements—the private Layer Scope does not erase that public requirement. The caller must provide that Scope.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

import { provideServiceEffect } from "@typed/fx/Fx";

Signatures

export declare const provideServiceEffect: {
    <Id, S, E2, R2>(tag: Context.Service<Id, S>, serviceEffect: Effect.Effect<S, E2, R2>): <A, E, R>(fx: Fx<A, E, R>) => Fx<A, E | E2, Exclude<R, Id> | R2>;
    <A, E, R, Id, S, E2, R2>(fx: Fx<A, E, R>, tag: Context.Service<Id, S>, serviceEffect: Effect.Effect<S, E2, R2>): Fx<A, E | E2, Exclude<R, Id> | R2>;
};

Why

Some service instances require effectful configuration but do not warrant a separately named Layer. This keeps acquisition failure and dependencies explicit in the returned Fx type.

Ownership and lifetime

The service Effect runs once per subscription while {@link provide} builds its Layer. A failure prevents the source from starting. On success, the service is available for the whole source run and its identifier is removed from R; acquisition errors and every requirement in R2 are added. In particular, an Effect.acquireRelease service Effect still leaves Scope.Scope in the returned Fx requirements—the private Layer Scope does not erase that public requirement. The caller must provide that Scope.

Examples

import { Context, Effect } from "effect"
import { provideServiceEffect } from "@typed/fx/Fx"
import { fromEffect } from "@typed/fx/Fx"

class Config extends Context.Service<Config, { readonly url: string }>()("Config") {}
const request = fromEffect(Effect.map(Config, (x) => x.url))
const runnable = provideServiceEffect(request, Config, Effect.succeed({ url: "/api" }))
Scoped acquisition keeps `Scope` in the returned Fx requirements
```ts
import { Context, Effect } from "effect"
import { provideServiceEffect } from "@typed/fx/Fx"
import { fromEffect } from "@typed/fx/Fx"
import { collectAll } from "@typed/fx/Fx"

class Config extends Context.Service<Config, { readonly url: string }>()("Config") {}
const request = fromEffect(Effect.map(Config, (x) => x.url))
const acquired = Effect.acquireRelease(
Effect.succeed({ url: "/api" }),
() => Effect.void
)
const scopedFx = provideServiceEffect(request, Config, acquired)
const program = Effect.scoped(collectAll(scopedFx))
```

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source