Skip to content

Instantly share code, notes, and snippets.

@BashkaMen
Created July 1, 2020 10:58
Show Gist options
  • Save BashkaMen/cae6d639e1e7f59a36fa055828315f45 to your computer and use it in GitHub Desktop.
Save BashkaMen/cae6d639e1e7f59a36fa055828315f45 to your computer and use it in GitHub Desktop.
module CounterBot =
type State = int
type Msg =
| Increment
| Decrement
let init = 0
let update msg (state: State) : State =
match msg with
| Increment -> state + 1
| Decrement -> state - 1
let view (state: State) =
Reply.Text { Text = sprintf "Counter state: %i" state }
let dispatcher update state =
match update with
| Message (message, _) ->
match message with
| MessageModel.Text x when x.Text = "+" -> Ok Increment
| MessageModel.Text x when x.Text = "-" -> Ok Decrement
| _ -> Reply.Text { Text = "Я жду только '+' или '-'" } |> Error
module Root =
type State =
| NotStarted
| Counter of CounterBot.State
type Msg =
| OpenCounter
| CounterMsg of CounterBot.Msg
let init = NotStarted
let update msg state =
match state, msg with
| NotStarted, _ -> Counter <| CounterBot.init
| Counter counter, CounterMsg counteMsg -> Counter <| CounterBot.update counteMsg counter
| Counter _, OpenCounter -> state
let view state =
match state with
| NotStarted -> Reply.Text { Text = "Not started" }
| Counter c -> CounterBot.view c
let dispatcher update state =
match state, update with
| NotStarted, _ -> Ok OpenCounter
| Counter c, x -> CounterBot.dispatcher x c |> Result.map CounterMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment