Lookups
The examples run inside an active graph.
createLookup f affected source computes f state key for each key you read with Get. Unlike a projection, a lookup has no fixed key set: it creates cells for requested keys. For the common case of selection membership, use a selector. When the source changes, it recomputes live cells for the keys returned by affected prev next.
Cell lifetime
An unobserved cell is evicted on the next source transition or read of another key.
let stock = createSignal (Map [ "apples", 3; "pears", 0 ])
let changedKeys (prev: Map<string, int>) (next: Map<string, int>) =
Seq.append prev.Keys next.Keys
|> Seq.distinct
|> Seq.filter (fun k -> prev.TryFind k <> next.TryFind k)
let onHand =
createLookup (fun (m: Map<string, int>) k -> m.TryFind k |> Option.defaultValue 0) changedKeys (fun () -> stock.Value)
onHand.Get "apples", onHand.Get "plums"
(3, 0)
Change the apple count while the pear reader stays quiet. The lookup's affected function names only the key whose count changed.
Failed and pending lookup cells
A key whose computation throws holds the exception, and is recomputed on every later transition until it succeeds. A pending source suspends every live cell until it settles.