Created
June 19, 2020 13:18
-
-
Save weebs/3dcbc7179e76a9e441f630bb3b180bdd to your computer and use it in GitHub Desktop.
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
module LensPatternElmish.Client.Main | |
open Elmish | |
open Bolero | |
open Bolero.Html | |
open Bolero.Templating.Client | |
type eLens<'a, 'model> = unit -> 'a * ('a -> 'model) | |
type updater<'b, 'a> = 'b -> 'a -> 'a * Cmd<'b> | |
let inline delegateTo (elens: eLens<'a, 'model>) (update: updater<'b, 'a>) (msgType: 'b -> 'msg) (msg: 'b) : 'model * Cmd<'msg> = | |
let localModel, lensSet = elens () | |
let updatedLocal, localCmd = update msg localModel | |
let a = lensSet updatedLocal | |
let b = Cmd.map msgType localCmd | |
a, b | |
let inline mySetL (elens: eLens<'a, 'model>) newLocal = | |
let (_, f) = elens () | |
f newLocal | |
// () | |
// lensSet updatedLocal, Cmd.map msgType localCmd | |
type FooModel = | |
{ | |
Count: int | |
} | |
type FooMessage = | |
| Increment | |
| Decrement | |
let updateFoo msg model = | |
match msg with | |
| Increment -> { model with Count = model.Count + 1 }, if model.Count % 2 = 0 then Cmd.none else Cmd.ofMsg Increment | |
| Decrement -> { model with Count = model.Count - 1 }, Cmd.none | |
type Component() = | |
inherit ElmishComponent<FooModel, FooMessage>() | |
override this.View model dispatch = | |
div [] [ | |
button [ on.click (fun _ -> dispatch Increment) ] [ text "+" ] | |
input [ attr.value (model.Count.ToString()) ] | |
button [ on.click (fun _ -> dispatch Decrement) ] [ text "-" ] | |
] | |
type Model = | |
{ | |
Foo: FooModel | |
x: string | |
} | |
static member init() = { Foo = { Count = 0 }; x = "" } | |
member this.foo() = this.Foo, fun value -> { this with Foo = value } | |
static member inline _foo f model = f model.Foo <&> fun value -> { model with Foo = value } | |
type Message = | |
| Ping | |
| FooMsg of FooMessage | |
//let z = fk2 initModel.foo updateFoo FooMsg | |
// | |
let update message (model: Model) = | |
match message with | |
| Ping -> model, [] | |
| FooMsg m -> delegateTo model.foo updateFoo FooMsg m | |
let view model dispatch = | |
div [] [ | |
p [] [ text "Hello!" ] | |
ecomp<Component,_,_> [] model.Foo (dispatch << FooMsg) | |
] | |
type MyApp() = | |
inherit ProgramComponent<Model, Message>() | |
override this.Program = | |
Program.mkProgram (fun _ -> Model.init(), []) update view | |
#if DEBUG | |
|> Program.withHotReload | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment