Graphs
Every node belongs to a Graph.
new Graph() uses GraphOptions.Default. Check the reference section to see what the default options are for your current release.
The Active Graph
The standard API surface will resolve the graph through Graph.Current, this peeks at the calling threads Graph stack.
Example
This code block is actively asserted on every build of the docs.
// Creating a graph does not automatically activate it
let graph = new Graph()
let graph1 = new Graph()
assert Graph.TryCurrent.IsNone
// You can manually activate a graph
let active = graph.Activate() : System.IDisposable
assert (Graph.Current = graph)
// The next graph is appended to the stack
let active1 = graph1.Activate()
assert (Graph.Current = graph1)
// The `Run` member activates the graph, runs the body, and
// then pops the graph from the stack.
graph.Run(fun () ->
assert (Graph.Current = graph)
)
assert (Graph.Current = graph1)
// Dispose an 'activate' instance to pop it from the active
// graph stack
active1.Dispose()
assert (Graph.Current = graph)
Threads and Graphs
The active graph is per thread.
A graph activated on one thread is not active on another.
let inner, restored, onOtherThread =
use outerGraph = new Graph ()
use innerGraph = new Graph ()
use _ = outerGraph.Activate ()
let inner =
use _ = innerGraph.Activate ()
obj.ReferenceEquals (Graph.Current, innerGraph)
let restored = obj.ReferenceEquals (Graph.Current, outerGraph)
let mutable onOtherThread = true
let thread = Threading.Thread (fun () -> onOtherThread <- Graph.TryCurrent.IsSome)
thread.Start ()
thread.Join ()
inner, restored, onOtherThread
printfn "inner active: %b, outer restored: %b, visible on another thread: %b" inner restored onOtherThread
inner active: true, outer restored: true, visible on another thread: false
C#
Graph.Run is an F# extension member.
From C#, Ranvier.CSharp provides it as an extension method; see C#.