Skip to content

Effects, bindings and bridges

Use an effect, a binding, or a cross-graph bridge when reactive state needs to drive external work rather than produce a cached value for downstream readers (for derived values, see Memos and computation expressions).

Effects

effect connects reactive inputs to side effects such as logging, DOM mutations, or external subscriptions. With Phase-1 let! / and! binds, the compiler builds a static split effect (EffectOn): Phase 1 reads and compares the settled key, and Phase 2 runs untracked inside an owning scope only when that key moves.

open Ranvier
open Ranvier.CE

let attachSubscription (channel: Signal<string>) (page: Signal<int>) (subscribe: string -> int -> System.IDisposable) =
    effect {
        let! name = channel |> withComparer System.StringComparer.OrdinalIgnoreCase
        and! number = page
        let sub = subscribe name number
        cleanup { sub.Dispose () }
    }

Choose the effect shape that matches your dependency structure:

  • Static Phase-1 key (let! ... and! ...): resolves source.Graph directly without graph.Activate (). See The graph — Graph inference.

  • When the action runs: an equal key (under the graph policy or withComparer cmp), a Pending input, or a Failed input leaves the previous Phase-2 action and its cleanups in place.

  • Timing: add debounce or throttle* in the header to gate rapid key bursts (see Equality, buffers and timing).

  • Dynamic reads: sequential let! chains (let! k = a followed by let! v = lookup k) or bindless effect { ... } / createEffectre-collect dependencies dynamically on each run.

  • Previous values: effect has no previous token. When an action needs (prev, current) deltas, compute them upstream with memo.previous or memo.previousOr seed in a memo.

Flush timing, cleanups, and handles

Outside a batch or an active flush, an effect runs immediately on creation, and writes flush dependent effects synchronously before signal.Value <- v returns.1 Inside batched { ... }, woken effects wait until the outermost batch exits (see Lifetimes and scopes).

  • Cleanups (cleanup { ... } / onCleanup): registered LIFO on the current run's scope, running untracked before the next action run and when the enclosing owner is disposed.2

  • Inspection handle: effect returns unit by default. Add the node header (effect { node; ... }, or createEffectNode) to obtain an INode for inspecting Id and Status (such as Status.Stale inside a batch).

  • Effect instance: construct new Effect (graph, body) when you need Runs, Error, or independent Dispose ().

  • Feedback writes: an effect that writes a signal it reads re-runs in the active flush until the value converges.3

One-source bindings

When a view property or external sink copies a single Signal<'T> or Memo<'T>on the same graph, bindSignal source target write and bindMemo source target write attach a fixed single-edge observer in O(1)O(1) without dynamic source-list tracking:5

open Ranvier
open Ranvier.CE

type Label () =
    member val Text = "" with get, set

let wireLabel (g: Graph) =
    g {
        let name = signal { return "Ada" }
        let label = Label ()
        bindSignal name label (fun target value -> target.Text <- value)
        name.Value <- "Grace"
        return label.Text // "Grace"
    }
  • Untracked leaf writer: write runs untracked on the next flush (coalescing multiple writes inside a batched block into one write of the latest value) and must not create reactive nodes or register cleanups.

  • Equality cutoff: source's equality policy or per-node comparer suppresses equal writes before write runs.6

  • Unready memos: while a Memo<'T> is Pending or Failed, target retains its last written value.

  • Lifetime: disposed automatically with the enclosing owner.7

Compare effect and binding workloads in Benchmarks → Effects.

Cross-graph bridges

A worker graph can forward derived state to a Signal<'T> on another graph or thread without allocating per-event closures. For a write-only target, obtain a SignalPort<'T>with target.CreatePort () or Signal.createPort target.

Forward one source

bindSignalTo source target (SignalPort.bindSignal source port) and bindMemoTo source target (SignalPort.bindMemo source port) resolve source.Graph directly without Activate (). See The graph — Graph inference.

The forwarders ignore Pending / Failed / equal memo states, and coalesce off-thread bursts in O(1)O(1) time and 0 B steady-state memory.

Project multiple sources

Use bridge target { ... } to combine tracked Phase-1 inputs (let!, and!, withComparer, tryRead) with a Phase-2 projection that runs with tracking closed.

  • Input cutoff: an equal Phase-1 key skips the projection.

  • Output cutoff: before posting, the bridge compares the projected value using comparer cmp or the source graph's equality policy.8 Equal outputs leave the target dispatcher asleep.

  • Timing: debounce and throttle* headers rate-limit Phase-1 bursts on the source graph. Use clock to supply a clock.

  • Inspection: the node header returns the bridge as an INode.

let connectWorker (workerA: Signal<int>) (workerB: Signal<int>) (uiSummary: SignalPort<string>) =
    bridge uiSummary {
        comparer System.StringComparer.Ordinal
        throttleLast (System.TimeSpan.FromMilliseconds 16.)
        let! a = workerA
        and! b = workerB
        return $"total = {a + b}"
    }

For FIFO ordered commands or multi-signal atomic transactions across threads, use dispatch instead; see Streams, threading and multi-graph dispatch.

Watch cross-graph cutoff on the signal map

worker runs as a separate peer graph feeding uiTick through gate. Press Minor tick (14) to see Phase 2 project 14 / 10 = 1 and suppress the cross-graph post; press Next bucket (23) to see 2 surge across the dashed bridge link into uiTick:

Example code
let worker = peerGraph "worker"
let rawTick = Trace.named "rawTick" (fun () -> worker { return signal { return 10 } })
let uiTick = signal { return 0 }
let gate =
    bridge uiTick {
        node
        let! x = rawTick
        return x / 10
    }
let label =
    memo {
        let! b = uiTick
        return $"Bucket {b}"
    }
effect {
    let! s = label
    printfn "%s" s
}

controls [
    button "Minor tick (14)" (fun () -> rawTick.Value <- 14)
    |> describe "Bucket stays 1: bridge suppresses the cross-graph post."
    |> expect "uiTick stays at 1" (fun () -> uiTick.Peek = 1 && label.Peek = "Bucket 1")
    button "Next bucket (23)" (fun () -> rawTick.Value <- 23)
    |> describe "Bucket moves to 2: bridge surges across the dashed graph link."
    |> expect "uiTick updates to 2" (fun () -> uiTick.Peek = 2 && label.Peek = "Bucket 2")
]
Hover a node for its state; click it for why it last ran.

Edit this page