# Fable (JavaScript)

> Compiling Ranvier to JavaScript with Fable (fickl) and runtime differences from .NET.

Ranvier compiles its F# engine directly to JavaScript via [Fable](https://fable.io)
(`dotnet fickl`) and runs its shared test suite against Node.js in both untraced and `.Traced`
builds.

`.NET` is the reference implementation; the tables below summarise packaging and target
differences when sharing code across `.NET` and browser/Node.js hosts.

## Packaging and setup

`Ranvier`, `Ranvier.Elmish`, `Ranvier.Query`, `Ranvier.Wire` (`0.2.7`), `Fable.Ranvier.Dom`
(`0.1.2`), and the sibling `Ranvier.View` packages — along with their `.Traced` variants — embed
Fable sources and a standalone `fable/<package>.fsproj`.

Core packages do not pin `Fable.Core`; reference `Fable.Core` in your Fable project alongside
`Ranvier$(RanvierSuffix)` and compile with `dotnet fickl`:

```xml
<PropertyGroup>
  <RanvierTrace Condition="'$(RanvierTrace)' == '' and '$(Configuration)' == 'Debug'">true</RanvierTrace>
  <RanvierSuffix Condition="'$(RanvierTrace)' == 'true'">.Traced</RanvierSuffix>
  <DefineConstants Condition="'$(RanvierTrace)' == 'true'">$(DefineConstants);RANVIER_TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="Fable.Core" Version="5.3.0" />
  <PackageReference Include="Ranvier$(RanvierSuffix)" Version="0.2.7" />
</ItemGroup>
```

```fsharp
open Ranvier
open Ranvier.CE

use graph = new Graph ()

graph {
    let count = signal { return 1 }
    let doubled = memo { let! n = count in return n * 2 }

    effect {
        let! d = doubled
        printfn "%d" d
    }

    count.Value <- 5 // prints 2, then 10 on both .NET and Fable
}
```

See [Getting started](getting-started.md#from-nuget) for package versions and
[Ranvier.View](https://ranvier-org.github.io/Ranvier.View/view/) for browser mounting, server
HTML, hydration, and document-head ownership (or build custom UI adapters on
[fixed-dependency bindings](effects-and-bindings.md#one-source-bindings)).

## Supported surface

The shared Node.js test suite (`dotnet fsi --exec build.fsx -- test-fable`) verifies all core
reactive primitives under inline and promise delivery:

| Area | Supported under Fable | Target-specific adapters |
| --- | --- | --- |
| Core & `Ranvier.CE`{fsharp} | `signal`{fsharp}, `ReadOnlySignal`{fsharp}, `SignalPort`{fsharp}, `memo`{fsharp}, `memoTable`{fsharp}, `effect`{fsharp}, `editable`{fsharp}, `draft`{fsharp}, `boundary`{fsharp}, `batched`{fsharp}, `untracked`{fsharp}, `cleanup`{fsharp}, `root`{fsharp} | Public `Memo<'T>`{fsharp} / `AsyncMemo<'T>`{fsharp} class constructors are `.NET`-only — use `Ranvier.CE`{fsharp} or `Api`{fsharp} factories (`createMemo*`{fsharp}, `createAsync*`{fsharp}) |
| Bindings & bridges | `bindSignalTo`{fsharp}, `bindMemoTo`{fsharp}, `SignalPort.bind*`{fsharp}, `bridge`{fsharp} | Single-threaded JS runs `SignalPort.Post`{fsharp} inline on the owning graph |
| Async & timing | `AsyncSource`{fsharp}, `asyncMemo`{fsharp} (all six `FlightPolicy`{fsharp} modes), `debounced*`{fsharp}, `throttled*`{fsharp} | `TimedClock.ofTimeProvider`{fsharp} is `.NET`-only |
| Streams | `stream`{fsharp} (`createStream*`{fsharp}, `createStreamFold*`{fsharp}), `StreamSource.create`{fsharp}, `ofObservable`{fsharp}, `ofAsyncIterable`{fsharp} | `ofAsyncIterable`{fsharp} is Fable-only; `ofAsyncEnumerable`{fsharp} and `ofChannel`{fsharp} are `.NET`-only ([Streams](streams-and-threading.md#streams)) |
| Collections | `KeyedCollection`{fsharp}, `Projection`{fsharp} (`row`{fsharp}), `IndexProjection`{fsharp}, `Lookup`{fsharp}, `Selector`{fsharp}, views & aggregates | `Projection.AsObservableCollection`{fsharp} is `.NET`-only |
| Extensions & diagnostics | `Ranvier.Elmish` (`Mvu`{fsharp}), `Ranvier.Query`, `Ranvier.Wire`, `Fable.Ranvier.Dom`, `Journal`{fsharp}, `Trace`{fsharp}, `Trace.dumpText`{fsharp} | `Trace.dump`{fsharp} (file I/O), `TraceModel.parseDump`{fsharp}, and `SynchronizationContextDispatcher`{fsharp} are `.NET`-only |

## Runtime differences from .NET

### Scheduling, promises, and journaling

| Behaviour | `.NET` | Fable (JavaScript) |
| --- | --- | --- |
| Graph dispatch & threading | Owning-thread affinity checks (`Guarded`{fsharp} / `Serialised`{fsharp}) and cross-thread inbox | Single-threaded: `IsOnGraphThread`{fsharp} is always `true`{fsharp}, `Dispatch`{fsharp} runs inline, `PendingWork`{fsharp} and `Pump ()`{fsharp} return `0`{fsharp}; affinity checks and locks are compiled out |
| Synchronous async completion | Pre-completed `Task<'T>`{fsharp} continuations run inline; first read is `Ready`{fsharp} | Every flight is a JS `Promise`: first read is `Pending`{fsharp} until the microtask queue drains (including during synchronous SSR) |
| Stream completion | Synchronous `Task`{fsharp} completion on the graph thread ends the stream before `SetResult`{fsharp} returns | Observed one microtask after `Run`{fsharp} settles; items delivered in that window still fold and publish |
| Tracking after `await` | Tracked only if the awaited `Task`{fsharp} had already completed synchronously | **Never tracked** — every `await` resumes on a microtask outside any computation frame |
| Journal attribution after `await` | `AsyncLocal`{fsharp} attributes effect / `Mvu`{fsharp} continuations automatically | Capture work before `await` with `Journal.work`{fsharp} and apply writes/settles via `Journal.produce`{fsharp} so replay records `Produced`{fsharp} instead of `External`{fsharp} ([Tracing and journal](tracing-and-journal.md#deterministic-journal-and-replay)) |
| Cancellation recognition | `OperationCanceledException`{fsharp} matching the flight token | Promise rejection whose reason is Fable's `OperationCanceledException`{fsharp} (e.g. cancelled `TaskCompletionSource`{fsharp}); other rejections become `Failed`{fsharp} |

### Equality, types, and trace details

| Topic | Target difference |
| --- | --- |
| **Value-type equality (`JsIdentityPolicy`{fsharp})** | Structs, struct tuples, `DateTime`{fsharp}, `DateTimeOffset`{fsharp}, `decimal`{fsharp}, and `KeyValuePair`{fsharp} compile to JS objects (reference-compared unless given a custom/structural comparer), whereas `Some x`{fsharp} erases to `x`{fsharp} (`Some 1`{fsharp} over `Some 1`{fsharp} cuts off under Fable). Under `StructuralPolicy`{fsharp}, `nan`{fsharp} cuts off on `.NET` and propagates under Fable ([Equality and timing](equality-and-timing.md)). |
| **Collection keys** | `ResizeArray`{fsharp} compiles to a JS `Array`{fsharp} (structural equality) under Fable vs reference identity on `.NET`; wrap collection keys in a class for identity on both targets. |
| **Unsettled `Peek`{fsharp}** | `Peek`{fsharp} before a memo's first run or an async memo's first settle returns `Unchecked.defaultof<'T>`{fsharp}, which is `null`{fsharp} in generic Fable code rather than `0`{fsharp} or `false`{fsharp}. |
| **Exception type tests** | `InvalidOperationException`{fsharp}, `ArgumentException`{fsharp}, and `ArgumentNullException`{fsharp} preserve their types; `ObjectDisposedException`{fsharp} and `KeyNotFoundException`{fsharp} compile to plain `Exception`{fsharp} under Fable. |
| **Null multi-argument functions** | Fable wraps multi-argument function values in closures, so `Unchecked.defaultof<_>`{fsharp} fails when called rather than raising `ArgumentNullException`{fsharp} eagerly on registration. |
| **Formatting & `for`{fsharp} bounds** | `string true`{fsharp} is `"true"`{fsharp} under Fable (`"True"`{fsharp} on `.NET`); format explicitly with `%b` / `%d` / `%s`. Bind `for i in a .. b`{fsharp} upper bounds to a `let`{fsharp} before the loop if `b`{fsharp} reads reactive state (Fable re-evaluates `b`{fsharp} each iteration). |
| **Traced log records** | Under Fable, `Trace.origin`{fsharp} has `Site = null`{fsharp} (paths fall back to node kind or `Trace.named`{fsharp} labels), failures record the message without exception type name, and values format via Fable's `%A` instead of `.NET` `ToString`{fsharp}. |
