function / @typed/fx

Subject.share

Shares one active execution of an Fx among subscribers through the supplied Subject.

Package version
2.0.0-beta.7
Category
Sharing sources
Since
1.0.0
Member of
Subject

Import

import { Subject } from "@typed/fx";

Access Subject.share through the imported Subject export. Its declaration below describes the member.

This public exposure is a re-export. Its import path is supported; declaration documentation is shared with the other public exposures below.

Signatures

export declare function share<A, E, R, R2>(fx: Fx.Fx<A, E, R>, subject: Subject<A, E, R2>): Fx.Fx<A, E, R | R2 | Scope.Scope>;

Why

Use share when the caller must choose the multicast boundary—for example, a replaying subject, a renderer-independent state object, or a service-backed subject—while ensuring that simultaneous consumers do not duplicate the source’s effects.

Subscription and publication

A subscriber is registered with the subject before the source starts, so the first subscriber observes synchronous first values and failures. The first active subscriber starts the source; later subscribers join that execution. When the last subscriber leaves, the source fiber is interrupted. A later subscriber starts a fresh session.

Errors, services, and interruption

Source successes and failures are pushed unchanged through the subject. The returned Fx requires the union of source services and subject services.

Ownership and lifetime

Scope owns each individual subscription. The shared source is finalized exactly once when the active session ends or the source itself exits. share takes session ownership of the supplied subject: source exit calls its interrupt, closing all of that subject’s subscribers and clearing retained state. Do not concurrently reuse the same subject for an independently owned producer or subscriber population.

Examples

import { Effect } from "effect"
import { Fx } from "@typed/fx"
import * as Subject from "@typed/fx/Subject"

const backing = Subject.unsafeMake<number>(1)
const request = Fx.fromEffect(Effect.succeed(42))
const shared = Subject.share(request, backing)

Other public imports

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

Source