-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update remoting to latest, docs for client #118
Changes from all commits
2e821fc
8668eb6
16def73
6038cd0
fc432b2
a7f1af9
e9d2b14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,18 @@ open Fulma | |
open Fulma.FontAwesome | ||
#endif | ||
|
||
type Model = Counter option | ||
|
||
// The model holds data that you want to keep track of while the application is running | ||
// in this case, we are keeping track of a counter | ||
// we mark it as optional, because initially it will not be available from the client | ||
// the initial value will be requested from server | ||
type Model = { Counter: Counter option } | ||
|
||
// The Msg type defines what events/actions can occur while the application is running | ||
// the state of the application changes *only* in reaction to these events | ||
type Msg = | ||
| Increment | ||
| Decrement | ||
| Init of Result<Counter, exn> | ||
| InitialCountLoaded of Result<Counter, exn> | ||
|
||
|
||
#if (remoting) | ||
|
@@ -32,39 +38,49 @@ module Server = | |
open Fable.Remoting.Client | ||
|
||
/// A proxy you can use to talk to server directly | ||
let api : ICounterProtocol = | ||
Proxy.remoting<ICounterProtocol> { | ||
use_route_builder Route.builder | ||
} | ||
let api : ICounterApi = | ||
Remoting.createApi() | ||
|> Remoting.withRouteBuilder Route.builder | ||
|> Remoting.buildProxy<ICounterApi>() | ||
|
||
#endif | ||
|
||
// defines the initial state and initial command (= side-effect) of the application | ||
let init () : Model * Cmd<Msg> = | ||
let model = None | ||
let cmd = | ||
let initialModel = { Counter = None } | ||
let loadCountCmd = | ||
#if remoting | ||
Cmd.ofAsync | ||
Server.api.getInitCounter | ||
Server.api.initialCounter | ||
() | ||
(Ok >> Init) | ||
(Error >> Init) | ||
(Ok >> InitialCountLoaded) | ||
(Error >> InitialCountLoaded) | ||
#else | ||
Cmd.ofPromise | ||
(fetchAs<int> "/api/init") | ||
[] | ||
(Ok >> Init) | ||
(Error >> Init) | ||
(Ok >> InitialCountLoaded) | ||
(Error >> InitialCountLoaded) | ||
#endif | ||
model, cmd | ||
|
||
let update (msg : Msg) (model : Model) : Model * Cmd<Msg> = | ||
let model' = | ||
match model, msg with | ||
| Some x, Increment -> Some (x + 1) | ||
| Some x, Decrement -> Some (x - 1) | ||
| None, Init (Ok x) -> Some x | ||
| _ -> None | ||
model', Cmd.none | ||
initialModel, loadCountCmd | ||
|
||
// The update function computes the next state of the application based on the current state and the incoming events/messages | ||
// It can also run side-effects (encoded as commands) like calling the server via Http. | ||
// these commands in turn, can dispatch messages to which the update function will react. | ||
let update (msg : Msg) (currentModel : Model) : Model * Cmd<Msg> = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer the shorter 6-line version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it was just my personal preference to do it this way 👌 |
||
match currentModel.Counter, msg with | ||
| Some x, Increment -> | ||
let nextModel = { currentModel with Counter = Some (x + 1) } | ||
nextModel, Cmd.none | ||
| Some x, Decrement -> | ||
let nextModel = { currentModel with Counter = Some (x - 1) } | ||
nextModel, Cmd.none | ||
| _, InitialCountLoaded (Ok initialCount)-> | ||
let nextModel = { Counter = Some initialCount } | ||
nextModel, Cmd.none | ||
|
||
| _ -> currentModel, Cmd.none | ||
|
||
|
||
let safeComponents = | ||
let intersperse sep ls = | ||
|
@@ -103,8 +119,8 @@ let safeComponents = | |
components ] | ||
|
||
let show = function | ||
| Some x -> string x | ||
| None -> "Loading..." | ||
| { Counter = Some x } -> string x | ||
| { Counter = None } -> "Loading..." | ||
|
||
#if (layout == "none") | ||
let view (model : Model) (dispatch : Msg -> unit) = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we float pin to a major version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but it is not necessary afaik