Sodium (Java, C#, cross-language)
A transactional push-based FRP library with a ten-primitive core over two types (Stream for discrete events and Cell for values that change in steps), in which every input opens a Transaction that propagates in rank order, without glitches, against a single consistent snapshot of every cell.
| Field | Value |
|---|---|
| Language | Java (primary here: nz.sodium, builds on Java 5+, lambdas from Java 8), C#/F# (dotnet/), Scala, Clojure, Haskell (deprecated), embedded C (experiment) in one repository; C++, Kotlin, TypeScript and Rust ports live in separate repositories |
| License | BSD 3-clause, "Copyright (c) 2012-2015, Stephen Blackheath and Anthony Jones" (COPYING) |
| Repository | SodiumFRP/sodium (README) |
| Documentation | Javadoc in the source; the book Functional Reactive Programming, Blackheath & Jones, Manning 2016 (manning.com); Denotational Semantics of Sodium 1.1 (Denotational.hs, PDF beside it) |
| Category | Discrete, push-based, transactional FRP (first-order Stream/Cell graph; higher-order via switchS/switchC) |
| Versions surveyed | master at 03d0c0fe446a (2026-09-28); Java 1.2.1-SNAPSHOT (pom.xml); the .NET tree at the same commit |
| Core abstraction | Stream<A> (event firings) and Cell<A> (a value at every instant), connected only inside a Transaction; StreamSink/CellSink are the only way in, listen the only way out |
NOTE
The Java port is the primary subject: it is described as "complete" in the README, it ships the nz.sodium.time timer system, and its tests are generated from the shared Haskell specification in common-tests/. The .NET tree is cited where it goes further: a Transaction contract written out in full, and the Sodium.Frp.Async MapAsync bridge with a SwitchLatest strategy. The README says the .NET port is "not in active development" and points at a successor, SodaFlow; the Async code is nevertheless present at the surveyed commit. The separately hosted ports (sodium-cxx, sodium-kotlin, sodium-typescript, sodium-rust) were not examined.
Overview
What it solves
Sodium is a working implementation of "classic" FRP's two-type model for imperative host languages. The README places it in a lineage: "This is based on Flapjax, Yampa, scala.React and a number of other Functional Reactive Programming efforts, as well as a lot of personal experience." It is meant for the middle of an application, meaning the logic between I/O sources (widgets, sockets, timers) and I/O sinks (rendering), and it draws that boundary itself: StreamSink is "an interface between the world of I/O and the world of FRP", and Stream.listen is "an OPERATIONAL mechanism … for interfacing between the world of I/O and for FRP" (Stream.java).
For this survey its value is that it gives a precise, specified answer to the question "what does one frame see?". The answer is: one transaction, one snapshot.
Design philosophy
- Two types, ten primitives, and a denotation. Events (
Stream) and step-wise values (Cell) are distinct types. The book's count of ten primitives (map,merge,hold,snapshot,filter,lift,never,constant,sample,switch) is from the book (not verified verbatim here). The executable denotational semantics defines exactly these as GADT constructors:MapS,Snapshot,Merge,Filter,SwitchS,Never,Constant,Hold,MapC,Apply,SwitchC, plus the operationalUpdates,Value,Split,Execute(Denotational.hs). - Transactions are the unit of time. "Transactions are serialized process-wide: at most one runs at a time, however many threads are involved. … It is what makes a transaction atomic with respect to every other thread - no observer can ever see the graph half-updated" (
.NET Transaction.cs). - Cell steps are not observable. Anything that exposes the moment a cell changes is quarantined in
Operational:updates"breaks the property of non-detectability of cell steps/updates. The rule with this primitive is that you should only use it in functions that do not allow the caller to detect the cell updates" (Operational.java). - Functions passed to primitives are pure. Every combinator's Javadoc says the function "must be referentially transparent", with one licensed exception: it "may construct FRP logic or use
Cell#sample()" (Stream.java).
Within this survey Sodium is the transactional discrete FRP data point. Compare Elm (the same model reduced to one update function per message), synchronous languages (where "one transaction" is "one reaction" and is compiled statically), and Solid signals / TC39 signals (glitch-free but with no event type). Vocabulary is in Concepts and the cross-cut in Comparison.
How it works
The objects
| Concept | Java type | Role |
|---|---|---|
| Event stream | Stream<A> (source) | A Node plus the firings seen in the current transaction, plus finalizers (cleanup listeners) |
| Step-wise value | Cell<A> (source) | An updates stream, the committed value, and a pending valueUpdate for the open transaction |
| Input | StreamSink<A>, CellSink<A> (source, cell sink) | send(a) opens (or joins) a transaction; refuses to run inside a callback |
| Forward reference | StreamLoop<A>, CellLoop<A> (source, cell loop) | Placeholder used before definition, resolved by loop(…) inside the same explicit transaction |
| Graph vertex | Node (source) | A rank and a list of Targets, each holding a weak reference to its handler |
| Clock of logic | Transaction (source) | A rank-ordered priority queue, a last queue, a post queue, and two global locks |
| Output handle | Listener (source) | unlisten(); append combines two |
A transaction, phase by phase
Transaction.close() (Transaction.java) runs three queues in a fixed order:
void close() {
while (true) { // 1. propagation, lowest rank first
checkRegen();
if (prioritizedQ.isEmpty()) break;
Entry e = prioritizedQ.remove();
entries.remove(e);
e.action.run(this);
}
for (Runnable action : lastQ) // 2. commit: hold() makes new values visible, firings cleared
action.run();
lastQ.clear();
// 3. postQ: each split()/defer() child index gets a NEW transaction;
// index -1 (Transaction.post) runs with no transaction at all.
}- Propagation.
StreamWithSend.sendrecords the firing and enqueues every listener target astrans.prioritized(target.node, …). The queue orders byNode.rank, with a sequence number breaking ties (Transaction.java). Ranks are kept topological byNode.linkTo, which callsensureBiggerThanon the target and its descendants. If the queue is non-empty when ranks change (aswitchrelinking mid-transaction),toRegenforces the heap to be rebuilt (Node.java). - Commit.
Cell's constructor listens to its stream and, on the first firing in a transaction, registerstrans2.last(…)to copyvalueUpdateintovalue(Cell.java). Until then,sample()returns the old value. This is the implicit delay ofhold: "[snapshot] always sees the value of a cell as it was before any state changes from the current transaction" (Stream.java). - Post.
Operational.splitputs each list element intopost_(childIx, …), andclose()runs each in a fresh child transaction. In the denotation, time is a list of integers (type T = [Int]) andsplitgives childnthe timet++[n](Denotational.hs), so the children are ordered after their parent and before the next external input.
Nesting is free: Transaction.run joins currentTransaction if one is open on the same thread, so a block wrapped in Transaction.runVoid(…) makes every send inside it simultaneous (Transaction.java).
Evaluation model
- Push, eager and hot. Constructing
s.map(f)immediately links a listener intos's node. Nothing is lazy exceptLazyinitial values (holdLazy,sampleLazy), which exist forCellLoops that are not yet bound (Cell.java). There is no subscription step that starts a producer, and no cold stream. - Glitch-free by construction. Rank ordering ensures that a node runs only after all its inputs for this transaction.
testLiftGlitchsends2intoa, liftsa*3witha*5, and asserts["3 5", "6 10"], never"6 5"(TestCell.java).Cell.apply, the primitive under everylift, defers its output with a nestedprioritizedcall and passes it throughlastFiringOnly, so one transaction yields one output (Cell.java). - Synchronous and batched per transaction.
sendreturns after propagation, commit and post have all run. External inputs on other threads block ontransactionLockfor the whole duration, callbacks included (.NET Transaction.cs: "the lock is held for the whole transaction, which includes every listener callback it fires"). - Simultaneity is a first-class notion. Two firings are simultaneous exactly when they are in the same transaction.
merge(s, f)guarantees "only ever to have one event per transaction" by coalescing simultaneous firings withf, andorElseismergewith(l, r) -> l(Stream.java). A bareStreamSinkthrows on a secondsendin one transaction: "send() called more than once per transaction, which isn't allowed. Did you want to combine the events? Then pass a combining function to your StreamSink constructor." (StreamSink.java).
IMPORTANT
The snapshot rule is the whole answer to "what does one frame see". Inside a transaction, every sample/snapshot reads the values committed at the end of the previous transaction. Every hold written during this one becomes visible together in the last phase. testHoldIsDelayed pins it down: e.snapshot(e.hold(0)) over sends 2, 3 yields ["2 0", "3 2"] (TestCell.java).
Composition
Beyond the primitives, the Java Stream/Cell surface is small, about 30 public combinators counting arity overloads once (Stream.java, Cell.java):
| Group | Operators |
|---|---|
| Primitives | Stream.map, merge(s, f), filter, hold, snapshot, new Stream() (never), new Cell(a) (constant), Cell.map, Cell.apply/lift (2–6 cells), sample, Cell.switchS, Cell.switchC |
| Derived | mapTo, orElse, filterOptional, gate(Cell<Boolean>), collect (Mealy machine), accum, once, snapshot over 2–5 cells, *Lazy variants |
| Operational | Operational.updates, value, defer, split (Operational.java) |
| I/O and lifetime | listen, listenWeak, listenOnce, addCleanup, Listener.append, Transaction.run/runVoid/post/onStart |
| Recursion | StreamLoop.loop, CellLoop.loop, legal only inside an explicit transaction (StreamLoop.java) |
Notably absent from core: distinctUntilChanged (the book's calm builds it from collectLazy, calm.java), debounce, flatMap/switchMap over async work, zip, buffer, retries, and any error operator. Higher-order composition is switchS/switchC over a Cell of streams/cells. The higher-order value is always a cell, so "which inner is current" is itself step-wise state that obeys the transaction rule.
switchS relinks at the end of the transaction, in trans2.last(…), and listens to the new stream with suppressEarlierFirings = true (Cell.java). The consequence is visible in testSwitchSSimultaneous: in the transaction that switches from ss3 to ss4, ss3's 8 is still output and ss4's 2 is not (TestCell.java). Switching therefore drops the old stream cleanly, but only at a transaction boundary. In the denotation the old stream's occurrences are kept for t <= t1, so the switch is inclusive of the switching instant (Denotational.hs).
Time and scheduling
Core has no clock. Time exists only as the sequence of transactions. Scheduling is fixed: one global serial order (rank order within a transaction, then post children), with no dispatcher, executor or frame concept.
nz.sodium.time (Java) and Sodium.Frp.Time (.NET) add an alarm system. The Scala port has no equivalent (scala/src/main/scala/sodium/ has no time module). The Java API (TimerSystem.java):
TimerSystem<T>(TimerSystemImpl<T>):timeis aCell<T>of clock time, andat(Cell<Optional<T>> tAlarm)is aStream<T>that fires when the clock reaches the cell's current alarm.TimerSystemImpl<T>(source) is the seam:setTimer(t, callback)returning a cancellableTimer,runTimersTo(t),now().MillisecondsTimerSystemusesSystem.currentTimeMillis()and a daemon thread that sleeps until the next deadline (MillisecondsTimerSystemImpl.java).SecondsTimerSystemis aDoubleof seconds since start (source).- .NET:
ITimerSystem<T>withTime(aBehavior<T>) andAt(Cell<Maybe<T>>)(ITimerSystem.cs). The book's WPF examples drive time from rendering frames:CompositionTargetTimerSystemsetsnowfromRenderingEventArgs.RenderingTimeon everyCompositionTarget.Rendering(source). This is a frame clock plugged into the same seam.
How alarms enter the graph. TimerSystem's constructor installs a Transaction.onStart hook, "The main use case of this is the implementation of a time/alarm system" (Transaction.java). Before every transaction it reads now(), runs due timers, and for each queued alarm at or before now sends the alarm's time into time and then into that alarm's StreamSink. Each of those sends is its own transaction, opened before the triggering one. When the timer thread's callback fires, it enqueues the event and "Open[s] and close[s] a transaction to trigger queued events to run" (TimerSystem.java). Consequences:
time.sample()is constant for the whole of a transaction. One transaction sees one instant.atrestarts itself: itlistens totAlarm, and on every change itcancel()s the currentTimerand sets a new one (or none, forOptional.empty()). A cell of deadlines is therefore a timer that can be reset, which is all a debounce needs.- The book's
periodictimer loopsat's output back into its own alarm cell through aCellLoop(timers.java).
Debounce is not provided anywhere in the repository. The only debounce in the tree is the book's web chapter, which uses RxJS's debounce(100) (autocomplete.js). Two Sodium constructions follow from the source:
// (a) Reset one alarm: a Cell<Optional<Long>> of deadlines; `at` cancels the old Timer.
CellLoop<Optional<Long>> deadline = new CellLoop<>(); // inside Transaction.runVoid
Stream<Long> sFire = sys.at(deadline);
deadline.loop(sQuery.map(q -> Optional.of(sys.time.sample() + delayMs.sample()))
.orElse(sFire.map(t -> Optional.<Long>empty()))
.hold(Optional.empty()));
Stream<String> sDebounced = sFire.snapshot(query);
// (b) Switch per event: a fresh alarm stream per keystroke; switchS drops the old one.
Stream<String> sDebounced2 = Cell.switchS(
sQuery.map(q -> sys.at(new Cell<>(Optional.of(sys.time.sample() + delayMs.sample())))
.mapTo(q))
.hold(new Stream<String>()));(b) is the idiom the brief anticipates, and it is correct as FRP. Operationally, though, it does not cancel the superseded timer. at cancels only when its alarm cell changes, and a constant cell never changes. The Listener inside at is released only by the dropped stream's finalize() (Stream.java), so each old alarm still fires and opens a transaction into a stream nobody listens to. That is a cost per keystroke that waits on GC (derived from the source, not executed). (a) cancels eagerly and is the better form.
Runtime-adjustable N. Both forms read delayMs when they arm, so a new N applies from the next restart. To make a change of N re-arm a pending window, snapshot the deadline cell (see the worked pipeline). Because of the hold delay, the delay change must use the event's own value, not delayMs.sample(), which in that transaction still returns the old N.
Concurrency and cancellation
Sodium has no concurrency inside the graph. Every transaction holds the single transactionLock, and listenersLock protects only the listener lists (Transaction.java). Any thread may send. Threads are serialized, not parallel. Blocking inside a callback blocks every input in the process, and the .NET documentation warns that "a callback which blocks waiting on a thread that is itself trying to start a transaction will deadlock" (.NET Transaction.cs).
Latest-wins inside FRP is switchS: switching unlistens the previous inner stream in the last phase (Cell.java). That handles stream identity but not work. A Sodium stream has no producer to stop, so switching cannot cancel anything that runs outside the graph.
CPU-bound work lives outside FRP, behind the I/O boundary. The book's canonical bridge is Lookup.lookup (Lookup.java): listenWeak on the request stream starts a Thread per request, and the thread ends with sDefinition.send(def) into a StreamSink, which is legal because the thread is outside any callback. IsBusy derives the busy cell as sOut.map(i -> false).orElse(sIn.map(i -> true)).hold(false). The book example has no cancellation and no staleness guard: an older reply that arrives later overwrites a newer one.
.NET MapAsync packages the bridge with a cancellation strategy (MapAsyncExtensions.cs, MapAsyncUtility.cs). It runs Func<TInput, CancellationToken, Task<TResult>> per firing, takes results and errors StreamSinks, exposes IsRunning/Items cells, and takes a strategy (Parallel, Queue, QueuePerGroup, SwitchLatest) (AsyncConcurrencyStrategy.cs). SwitchLatest's Admit calls state.Active?.Cancel() on the superseded item, and its OnCompleted publishes only isCurrent = state.Active != null && state.Active.Id == item.Id. This is the request-id guard, built into the library.
Does a cancel wait? No. Cancellation is a signal, and the guard is what makes it safe: "an operation that ignores it still runs to completion, and cancellation only means its result goes unpublished". Disposing likewise "never gags the pipeline: whatever is still in flight runs to completion" (MapAsyncUtility.cs). MapAsync's own state needs no lock because "Sodium serializes ALL transactions process-wide behind one global lock" (same file). Here the global lock doubles as the synchronization primitive for the bridge.
Errors and completion
- No error channel. Neither
StreamnorCellcan fail or complete. The denotation has no termination: a stream is an infinite list of(T, a)occurrences (Denotational.hs). Errors are values (Optional,Either, a result record), and the async bridge carries them on a second stream (MapAsync'serrors). - Java swallows callback exceptions.
StreamWithSend.sendwraps each handler intry { … } catch (Throwable t) { t.printStackTrace(); }(StreamWithSend.java). A throwingmapfunction does not abort the transaction. It is logged, and that branch simply does not fire. - .NET propagates.
TransactionInternal.Runcaptures the body's exception, still closes the transaction, and rethrows, combining the two in anAggregateExceptionif closing also throws (Core Transaction.cs).TimerSystemtakes anAction<Exception> handleExceptionfor its timer thread (TimerSystem.cs). - Completion substitutes.
once()yields a stream that fires at most once (Stream.java). The book'sPromisebuilds a one-shot value fromonce+hold(Optional.empty())(Promise.java). "Done" is a state you model, not a protocol.
State and sharing
Cellis the state primitive.hold,accum,collectandCellSinkare the only stateful constructions. ACellalways has a value (new Cell(a),hold(init)), so there is no "no value yet" state for consumers to handle.- Everything is multicast. A stream is one
Nodewith NTargets. Adding a listener never re-runs upstream logic, so the cold/hot andshare()questions do not arise. - Conflation is per transaction and explicit. Simultaneous firings are combined by
merge's function or a sink's combining function.CellSinkdefaults to "the last one is used" (CellSink.java), andCellkeeps onevalueUpdateper transaction. Across transactions nothing is ever dropped. - Late listeners are consistent. A listener added in a transaction that has already fired receives those firings (
firingsreplay inStream.listen, "so that there's no order dependency between send and listen",Stream.java).Cell.listenisvalue(trans).listen, which delivers the current value first (Cell.java).
Lifetime and ownership
- Listener-rooted, GC-collected.
Node.Targetholds its handler through aWeakReference(Node.java).Stream.listenkeeps the handler alive by adding theListenerto a statickeepListenersAliveset untilunlisten()(Stream.java). The graph is therefore owned by whoever holds aListener. Intermediate nodes stay alive through strong references from downstream to upstream, and they are torn down byfinalize()running each stream'sfinalizers. - Two listen flavours.
listenis a leak if you forgetunlisten().listenWeakunregisters when collected, andaddCleanupmust be used only withlistenWeaklisteners "so that things don't get kept alive when they shouldn't" (Stream.java). - No scope. There is no structured owner:
Listener.appendis the only aggregation (Listener.java). Cleanup of operational resources (threads, timers) is timed by the GC.MemoryTest1–5are soak programs that print heap size, not assertions (MemoryTest1.java). TheREADMEnotes that the C++ port's "memory management is not quite right yet". - Global hooks never unregister.
Transaction.onStartappends to a static list with no removal API (Transaction.java), so everyTimerSystemever created keeps running its hook before every transaction.
Testing
- Determinism comes free.
sendis synchronous and transactions are serialized, so a test is a list of sends followed by an assertion on collected outputs. EveryTestStream/TestCellcase has that shape (TestStream.java,TestCell.java). - A cross-language specification.
common-tests/holds semantic and operational tests in Haskell, andcommon-tests.hsgeneratesjava/src/test/java/nz/sodium/TestCommon.java("DO NOT EDIT THIS FILE") (common-tests.hs,TestCommon.java). The denotational semantics is executable, and .NET has aDenotationalSemanticsTests.cs(source). - Virtual time is possible but not shipped.
TimerSystem's constructor is public and takes anyTimerSystemImpl<T>, so a test implementation with a settablenow()and a sorted timer set gives a deterministic clock. Advancing it is "setnow, then open an empty transaction". The shipped implementations are wall-clock. The only timer test (TimerTests.SimultaneousTimerEvents) usesThread.Sleep(200)against the system clock (TimerTests.cs). No debounce test exists, because no debounce exists.
The worked pipeline
Query field → debounce N ms (N adjustable) → hash a file set on a worker with latest-wins cancellation → show the result, all behind an on/off toggle. Java 17 with Sodium (records are used for brevity; Sodium itself targets Java 8):
import nz.sodium.*;
import nz.sodium.time.*;
import java.util.Optional;
import java.util.concurrent.*;
record Req(long id, String query) {}
record Res(long id, Optional<String> digest, Optional<Throwable> error) {}
final class HashPipeline {
// I/O → FRP. Combining functions make "several inputs in one frame" legal (last wins).
final StreamSink<String> sQuery = new StreamSink<>((l, r) -> r);
final StreamSink<Long> sDelayMs = new StreamSink<>((l, r) -> r);
final StreamSink<Boolean> sEnabled = new StreamSink<>((l, r) -> r);
final StreamSink<Res> sResult = new StreamSink<>((l, r) -> r); // fed by the worker
final Cell<String> view; // what the frame renders
final Listener io; // keeps the operational side alive; unlisten() = dispose
HashPipeline(TimerSystem<Long> sys, ExecutorService pool) {
Listener[] keep = new Listener[1];
view = Transaction.run(() -> { // CellLoop needs an explicit transaction
Cell<String> query = sQuery.hold("");
Cell<Long> delayMs = sDelayMs.hold(250L);
Cell<Boolean> enabled = sEnabled.hold(true);
Lambda1<Long, Optional<Long>> in = n -> Optional.of(sys.time.sample() + n);
// 1. Debounce: a cell of deadlines; `at` cancels the old Timer on every change.
CellLoop<Optional<Long>> deadline = new CellLoop<>();
Stream<Long> sFire = sys.at(deadline);
Stream<Optional<Long>> sArm =
sEnabled.map(on -> on ? in.apply(delayMs.sample()) : Optional.<Long>empty())
.orElse(sQuery.gate(enabled).snapshot(delayMs, (q, n) -> in.apply(n)))
.orElse(Stream.filterOptional( // new N re-arms a PENDING window
sDelayMs.snapshot(deadline, (n, d) -> d.map(x -> in.apply(n)))))
.orElse(sFire.map(t -> Optional.<Long>empty()));
deadline.loop(sArm.hold(Optional.empty()));
Stream<String> sDebounced = sFire.snapshot(query);
// 2. Number the requests; `latest` is the only id whose result may be shown.
Stream<Req> sReq = sDebounced.collect(0L, (q, n) -> new Tuple2<>(new Req(n + 1, q), n + 1));
Stream<Boolean> sOff = sEnabled.filter(on -> !on);
Cell<Long> latest = sOff.map(off -> -1L).orElse(sReq.map(Req::id)).hold(-1L);
// 3. The bridge (operational): start a job, interrupt its predecessor.
Future<?>[] running = { null }; // touched only under the transaction lock
Listener lReq = sReq.listen(r -> {
if (running[0] != null) running[0].cancel(true);
running[0] = pool.submit(() -> {
Res res;
try { res = new Res(r.id(), Optional.of(hashFileSet(r.query())), Optional.empty()); }
catch (Throwable t) { res = new Res(r.id(), Optional.empty(), Optional.of(t)); }
sResult.send(res); // worker thread: its own transaction
});
});
Listener lOff = sOff.listen(off -> { if (running[0] != null) running[0].cancel(true); });
keep[0] = lReq.append(lOff);
// 4. Drop stale results; derive what to show.
Stream<Res> sFresh = Stream.filterOptional(sResult.snapshot(latest,
(res, id) -> res.id() == id ? Optional.of(res) : Optional.<Res>empty()));
Cell<Boolean> busy = sOff.map(off -> false).orElse(sFresh.map(r -> false))
.orElse(sReq.map(r -> true)).hold(false);
Cell<String> shown = sFresh.map(r -> r.digest()
.orElseGet(() -> "error: " + r.error().get())).hold("");
return shown.lift(busy, enabled, (s, b, on) -> !on ? "(off)" : b ? "hashing…" : s);
});
io = keep[0];
}
}Precise trace (MillisecondsTimerSystem, N = 250, times in ms). "T(x)" means one transaction:
| t | Event | What runs |
|---|---|---|
| 0 | construction | One transaction builds the graph. at listens to deadline, sees Optional.empty() and sets no timer. view = "". |
| 100 | sQuery.send("a") | The onStart hook first runs T(time := 100). Then T(query): gate passes, snapshot reads delayMs = 250 and sArm fires Some(350). at's listener (an updates listener, so it runs during propagation) calls setTimer(350). In the last phase query := "a" and deadline := Some(350). |
| 200 | "ab" (during debounce) | sArm → Some(450). at calls cancel() on the 350 timer, removing it from the TreeSet, and sets 450. Nothing else changes: the debounce restart is a cell step, not a stream switch. |
| 450 | timer thread fires | The callback enqueues Event(450) and opens an empty T. Its onStart hook pops the event and runs T(time := 450) then T(alarm). In T(alarm): sFire → sDebounced = snapshot(query) = "ab" → sReq = Req(1, "ab"); sArm → empty (alarm cleared); latest := 1 and busy := true at commit. During propagation, lReq submits job 1 and returns at once. view = "hashing…". |
| 600 | "abc" (during hash) | T: sArm → Some(850). Job 1 keeps running: latest-wins applies when the next request exists, as with debounce → switchMap. |
| 850 | alarm | T(alarm): sReq = Req(2, "abc"). lReq calls Future.cancel(true) on job 1 (an interrupt, which does not wait) and submits job 2. latest := 2. |
| ~870 | job 1 sees interrupt | Its hashFileSet throws, and the worker sends Res(1, error) → T: snapshot(latest) reads 2, filterOptional drops it, and nothing visible changes. Without the id guard this error would have overwritten the screen. |
| 1100 | job 2 completes | T: Res(2, digest) passes the guard; shown := digest and busy := false at commit; view = digest. |
| 1150 | "abcd", then at 1200 sDelayMs.send(500) | 1150: deadline := Some(1400). 1200: the delay branch snapshots deadline = Some(1400) (pending) and re-arms with the event's n: Some(1700). delayMs.sample() would still return 250 in this transaction. Had no window been pending, d would be empty and nothing would re-arm. |
| 1300 | sEnabled.send(false) | T: sArm → empty, so at cancels the 1700 timer. sOff fires: latest := −1, busy := false, and lOff interrupts any running job. view = "(off)" (computed from the committed enabled = false). query keeps tracking keystrokes while off (only arming is gated). |
| 1400 | sEnabled.send(true) | T: sArm → Some(1400 + delayMs.sample()) = Some(1900); at 1900 the current query is hashed under a fresh id. |
| end | io.unlisten() | Stops starting jobs. The graph and the at timers are released only when the GC finalizes them. The onStart hook of the TimerSystem is never removed. |
Where it fits a per-frame UI. The fit is unusually direct. A frame is one transaction: wrap the frame's input events in Transaction.runVoid(() -> { … send … }), and every keystroke, toggle and N change in that frame becomes simultaneous, with combining functions deciding the winner. The default StreamSink would throw here, which is why the sinks above pass (l, r) -> r. Rendering then reads view.sample(), which is consistent by construction: it can never see busy from one transaction and shown from another. Two seams need work for a frame loop. (1) Wake-ups: alarms and worker results arrive on foreign threads as their own transactions, so the UI must be woken (view.listen(v -> wakeNow())) or, better, the TimerSystemImpl should be driven by the frame clock, as CompositionTargetTimerSystem does, with the next deadline fed to wakeIn. (2) Retained graph: Sodium builds the graph once and keeps it, while an immediate-mode widget tree is rebuilt every frame. The FRP graph must live in application state, outside the per-frame widget code.
Strengths
- A precise, specified notion of "the same instant". Transactions, the hold delay and simultaneous-merge rules are documented on each primitive, defined denotationally, and tested across ports from one generator.
- Glitch-freedom and consistent snapshots for free. Rank-ordered propagation plus a separate commit phase means no observer ever sees a half-updated graph.
- Tiny core, closed under composition. About ten primitives plus
switchS/switchCexpress state machines (collect), recursion (CellLoop) and dynamic topology. - Explicit simultaneity.
merge(s, f)and sink combining functions force a decision about colliding events instead of an arbitrary interleaving. A bare sink fails loudly. - A clean timer seam.
TimerSystemImpland anat(Cell<Optional<T>>)alarm whose reset is a cell step make a resettable debounce, and a frame-driven or virtual clock, small adapters. - A well-shaped async bridge (.NET).
MapAsync+SwitchLatest= cancel-on-supersede plus a publish guard keyed on item identity, withIsRunningas a glitch-free cell.
Weaknesses
- One global lock. Every transaction, callbacks included, is serialized process-wide. This gives correctness, but no parallelism inside the graph, and a blocking callback risks deadlock.
- No error or completion model. Java prints and swallows handler exceptions. Streams never end. Failure and "done" must be hand-modelled as values.
- Lifetime is GC- and finalizer-driven. There are no structured scopes.
finalize()is deprecated in modern Java. Timers created inside aswitchSkeep firing until collected. StaticonStarthooks are never removed. - No time operators. No
debounce/throttle/delayin any port examined. The book reaches for RxJS when it needs one. - Async work is outside the model. Sodium (outside .NET's add-on) offers nothing for starting, cancelling or correlating work. The book's
Lookuphas neither cancellation nor a staleness guard. - The hold delay is a sharp edge. A delay change must use the event value, not the cell, and
CellLoopmust be resolved inside the same explicit transaction. Both are documented, but easy to get wrong. - Fragmented, partly unmaintained ports. The .NET port is officially superseded by SodaFlow; four ports live in separate repositories.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Separate Stream and Cell types | Events and values have different semantics (occurrence vs value-at-time) | Two of everything (map, switch, listen); Operational.updates is needed to cross back |
| Transaction = logical instant; one global lock | Atomic, deterministic, glitch-free updates from any thread | No parallel propagation; callbacks must not block; the lock is the only concurrency primitive |
hold updates commit in a last phase | Every read in an instant sees one snapshot; loops (CellLoop) become well-defined | One-transaction delay surprises users; "new value now" needs the event's own value |
| Rank-ordered priority queue | Topological propagation without a separate scheduler | Rank fix-ups on relink (ensureBiggerThan) and a heap rebuild (toRegen) during dynamic switching |
| Simultaneous firings must be combined explicitly | No hidden interleaving; one event per stream per transaction | A plain StreamSink throws on a second send; batching a frame requires combining functions |
switchS/switchC switch at end of transaction | Switching is consistent with the snapshot rule; no double-firing | An inner stream cannot be swapped mid-instant; work behind a dropped stream is not cancelled |
No clock in core; TimerSystem via an onStart hook | Time is just another input; the clock is pluggable (wall, seconds, frame) | Hidden extra transactions before each one; hooks never unregister; no virtual clock shipped |
I/O only through sinks and listen | Keeps FRP code referentially transparent; side effects at the edges | Async work, cancellation and request correlation are user code (Java) or an add-on library (.NET) |
| Weak node references + finalizers for teardown | Graph nodes need no manual disposal; only Listeners are roots | Release timing depends on the GC; resources such as timers outlive their logical owner |
Sources
- SodiumFRP/sodium at
03d0c0fe446afadcb18c12b4f37ea679ad64eb97(2026-09-28):README·COPYING·java/pom.xml - Java core:
Stream.java·Cell.java·Transaction.java·Node.java·StreamWithSend.java·StreamSink.java·CellSink.java·StreamLoop.java·CellLoop.java·Operational.java·Listener.java - Java time:
TimerSystem.java·TimerSystemImpl.java·MillisecondsTimerSystemImpl.java·SecondsTimerSystemImpl.java - Java tests:
TestStream.java·TestCell.java·TestCommon.java·MemoryTest1.java·common-tests.hs - .NET:
Sodium.Frp/Transaction.cs·Sodium.Core.Frp/Transaction.cs·ITimerSystem.cs·TimerSystem.cs·TimerTests.cs·DenotationalSemanticsTests.cs·AsyncConcurrencyStrategy.cs·MapAsyncExtensions.cs·MapAsyncUtility.cs - Book companion code:
Lookup.java·Promise.java·timers.java·calm.java·CompositionTargetTimerSystem.cs·autocomplete.js - Semantics:
Denotational.hs(the PDF Denotational Semantics of Sodium - 1.1 sits beside it) - Stephen Blackheath and Anthony Jones, Functional Reactive Programming, Manning, 2016 — manning.com/books/functional-reactive-programming
- SodaFlow, named in the README as the .NET successor (not examined)
- Related in this survey: index · Concepts · Comparison · RxJS · Elm · Synchronous languages · Solid signals · TC39 signals · event-horizon spec