Skip to content

Instantly share code, notes, and snippets.

@MangelMaxime
Created August 14, 2016 19:38
Show Gist options
  • Save MangelMaxime/ed2b3e4b1497e6b66da9f6eaff00c12a to your computer and use it in GitHub Desktop.
Save MangelMaxime/ed2b3e4b1497e6b66da9f6eaff00c12a to your computer and use it in GitHub Desktop.
#r "../node_modules/fable-core/Fable.Core.dll"
#load "libs/Parser.fsx"
#load "Helpers.fsx"
namespace Herebris.BridgeStream
open Fable
open Fable.Import
open Fable.Import.Browser
open Fable.Helpers.Virtualdom
open Fable.Helpers.Virtualdom.Html
open System
open Parser.RouteParser
open Parser.Parsing
module Main =
type Route =
| Home of string
| Add of int * int
| Sub of int * int
type Model =
{ CurrentRoute: Route option
ValueA: int
ValueB: int }
type Action =
| Sum of int * int
| Sub of int * int
| NewRoute of Route option
| PushRoute of Route
let update model action =
match action with
| NewRoute route ->
console.log "new route"
{ model with CurrentRoute = route }, []
| _ -> model, []
let view model =
div
[]
[
text "coucou"
button
[ onMouseClick (fun _ -> PushRoute (Add (3,5))) ]
[ text "Add 3 + 5" ]
]
let routes : (string -> Result<Route>) list =
[ runM Home (pstring "" |> _end)
runM Add (pstring "add" </.> pint </> pint |> _end)
// Add the full reference to Sub otherwise the compiler
// complain about Route and Action types
runM Route.Sub (pstring "sub" </.> pint </> pint |> _end) ]
let navigationOnChangeProducer h =
window.addEventListener_hashchange(fun _ ->
let newRoute =
window.location.hash.Substring 1 // Remove the first char because we use hash url in this sample
|> choose routes
h (NewRoute newRoute)
null
)
let navigationSubscriber appEvent =
match appEvent with
| ActionReceived msg ->
match msg with
| PushRoute route ->
match route with
| Add (a,b) ->
let newUrl = sprintf "/#add/%i/%i" a b
location.assign newUrl
| _ -> ()
| _ -> ()
| _ -> ()
createApp { CurrentRoute = None; ValueA = 0; ValueB = 0 } view update
|> withStartNodeSelector "#app"
|> withProducer navigationOnChangeProducer
//|> withProducer (fun h -> navigationPushAgent.Post(AddHandler h))
|> withSubscriber "navigation-subscriber" navigationSubscriber
|> start renderer
|> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment