Last active
December 15, 2017 05:56
-
-
Save ReedCopsey/7b871ed9f0f5c7f99cdc3190ace39d13 to your computer and use it in GitHub Desktop.
FsAdvent 2017 Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let application nav = | |
// Start pruning "loop" via Executor type | |
let prune = new Executor<_,_>(pruneHandler) | |
prune.Start() | |
// Start our application, and attach the executor so messages dispatch to the application | |
Framework.application Forest.empty Forest.update forestComponent nav | |
|> Framework.withDispatcher prune |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create binding for entire application. This will output all of our messages. | |
let forestComponent = | |
Component.create<Forest,unit,ForestMessage> [ | |
<@ forestDesign.Forest @> |> Bind.collection id treeComponent UpdateTree | |
<@ forestDesign.Add @> |> Bind.cmdParam Add | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let main _ = | |
// Run using the WPF wrappers around the basic application framework | |
let nav = Gjallarhorn.Wpf.Navigation.singleView System.Windows.Application MainWindow | |
let app = Program.application nav.Navigate | |
Gjallarhorn.Wpf.Framework.RunApplication<Forest,unit,ForestMessage> (nav, app) | |
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pruneHandler (dispatch : Dispatch<_>) token = | |
// Handle pruning of the forest - | |
// Twice per second, send a prune message to remove a tree if there are more than max | |
let rec pruneForever max = | |
async { | |
do! Async.Sleep 500 | |
Prune max |> dispatch | |
return! pruneForever max | |
} | |
// Start prune loop in the background asynchronously | |
Async.Start(pruneForever 10, token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let treeComponent = | |
Component.create<Tree,unit,TreeMessage> [ | |
<@ treeDesign.Tree @> |> Bind.oneWay id | |
<@ treeDesign.Decorate @> |> Bind.cmd | |
<@ treeDesign.Light @> |> Bind.cmd | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TreeVM = | |
{ | |
Tree : Tree | |
Decorate : VmCmd<TreeMessage> | |
Light : VmCmd<TreeMessage> | |
} | |
let treeDesign = { Tree = { Position = { X = 0.0 ; Y = 0.0 } ; Height = 1.0 ; Decorated = true ; Lit = true } ; Decorate = Vm.cmd Decorate ; Light = Vm.cmd Light } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment