class / @typed/fx

Subject.Share

The concrete lazy Fx returned by share.

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 class Share<A, E, R, R2> implements Fx.Fx<A, E, R | R2 | Scope.Scope> {
    readonly [FxTypeId]: Fx.Fx.Variance<A, E, R | R2 | Scope.Scope>;
    _FxFiber: MutableRef.MutableRef<Option.Option<Fiber.Fiber<unknown>>>;
    _RefCount: RefCounter;
    readonly i0: Fx.Fx<A, E, R>;
    readonly i1: Subject<A, E, R2>;
    constructor(i0: Fx.Fx<A, E, R>, i1: Subject<A, E, R2>);
    pipe(): unknown;
    run<R3>(sink: Sink.Sink<A, E, R3>): Effect.Effect<unknown, never, R | R2 | R3 | Scope.Scope>;
}

Why

Share exposes the ref-counted sharing mechanism as an ordinary Fx: one source execution feeds the selected subject while each caller has an independently scoped subscription to that subject. Most callers should prefer share, multicast, hold, or replay for clearer intent.

Ownership and lifetime

Construction acquires nothing. The first executing run starts a detached source fiber; the final executing run interrupts it. Source completion interrupts the subject’s current subscribers. Source, subject, and sink services are required only when run executes.

Constructor

Creates the lazy shared Fx; source acquisition waits until the first call to run executes.

Constructor: Why

Keeps selection of the source and multicast policy separate from their execution.

Constructor: Ownership and lifetime

The constructor only stores i0 and i1; it starts no fiber and installs no finalizer.

Property: [[computed:[FxTypeId]]]

Carries Fx variance information for the shared output, error, and service channels.

Property: [[computed:[FxTypeId]]]: Why

Lets generic Fx utilities recognize and infer a Share without inspecting its runtime fields.

Property: [[computed:[FxTypeId]]]: Ownership and lifetime

The marker is immutable metadata and acquires no resources.

Property: _FxFiber

Tracks the source fiber for the current active subscriber session.

Property: _FxFiber: Why

Gives the final departing subscriber an exact fiber to interrupt without restarting or duplicating the source.

Property: _FxFiber: Ownership and lifetime

The reference is empty before a session, set after the first subscriber starts the source, and cleared before or when that session ends. It does not outlive the Share instance.

Property: _RefCount

Tracks how many executing run effects participate in the active source session.

Property: _RefCount: Why

Implements the first-subscriber start and last-subscriber stop boundary.

Property: _RefCount: Ownership and lifetime

The counter is owned by the Share instance and never falls below zero. Each executing run increments once and decrements in its exit finalizer.

Property: i0

The source whose execution is shared.

Property: i0: Why

Retains the original source so every active session can start exactly that Fx.

Property: i0: Ownership and lifetime

Holding the Fx value starts no work. Its R services and cleanup participate only while a subscriber session is active.

Property: i1

The multicast subject receiving source publications.

Property: i1: Why

Separates source sharing from retention and publication policy.

Property: i1: Ownership and lifetime

Share does not allocate this subject. It subscribes sinks to it and interrupts it when the source session exits; the subject’s R2 services remain required by run.

Method: pipe

Returns this value through the concrete class’s zero-argument pipe entrypoint.

Method: pipe: Why

Supplies the runtime pipe hook required by Fx; the concrete Share declaration currently publishes only its zero-argument signature, so typed combinator composition should target the Fx returned by share, multicast, hold, or replay.

Method: pipe: Ownership and lifetime

Calling pipe itself acquires no resources; only an executing downstream run starts work.

Method: run

Registers sink, joins or starts the active source session, and remains active until the subscription ends. Interruption removes this subscriber and stops the source if it was last.

Method: run: Why

Implements Fx consumption while coordinating one source execution across independent sinks.

Method: run: Ownership and lifetime

The caller’s Scope owns its subject subscription. Source R, subject R2, and sink R3 services are required during execution. Source failures are published to the sink rather than failing this returned Effect, whose error channel is never.

Examples

import { Fx } from "@typed/fx"
import { Share, unsafeMake } from "@typed/fx/Subject"

const shared = new Share(Fx.succeed(1), unsafeMake<number>())
import { Fx } from "@typed/fx"
import { Share, unsafeMake } from "@typed/fx/Subject"

const shared = new Share(Fx.succeed("ready"), unsafeMake<string>())
import { Fx } from "@typed/fx"
import { Share, unsafeMake } from "@typed/fx/Subject"

const shared = new Share(Fx.succeed(1), unsafeMake<number>())
const same = shared.pipe()
import { Effect } from "effect"
import { Fx } from "@typed/fx"
import * as Sink from "@typed/fx/Sink"
import { Share, unsafeMake } from "@typed/fx/Subject"

const shared = new Share(Fx.succeed(1), unsafeMake<number>())
const run = shared.run(
  Sink.make(
    () => Effect.void,
    (value) => Effect.sync(() => console.log(value))
  )
)

Other public imports

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

Source