Skip to content

Commit 0189e4a

Browse files
committed
promises and passing app state around
1 parent fe6dd0a commit 0189e4a

File tree

6 files changed

+31
-45
lines changed

6 files changed

+31
-45
lines changed

fable/library.fs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open Fable.Core
55
open Fable.Core.JsInterop
66
open Fable.Import
77
open Types
8+
open Fable.PowerPack
89

910
module Util =
1011
let load<'T> key =
@@ -21,11 +22,8 @@ module Impl =
2122
console.log(person)
2223
person.age
2324

24-
[<Emit("Module.ccall('callback', 'void', ['string'], [$0]);")>]
25-
let callback(s:string) : unit = jsNative
26-
27-
let loadPerson() : unit =
28-
console.log( "loadPerson function called" )
29-
{ name="Jack Sparrow"; age=29; phones=[ "555 123 456"; "123 456 789" ] } |> toJson |> callback
30-
31-
let render(person:Person) = UI.render(person)
25+
let loadPerson() =
26+
promise {
27+
console.log( "loadPerson function called" )
28+
return { name="Jack Sparrow"; age=29; phones=[ "555 123 456"; "123 456 789" ] }
29+
}

fable/library.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
</ItemGroup>
5757
<ItemGroup>
5858
<Compile Include="types.fs" />
59-
<Compile Include="ui.fs" />
6059
<Compile Include="library.fs" />
60+
<Compile Include="ui.fs" />
6161
</ItemGroup>
6262
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
6363
<Choose>

fable/types.fs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,8 @@ module Types =
2525
type [<Pojo>] Props = {
2626
state : IState
2727
}
28-
with
29-
override this.ToString() =
30-
toJson this
31-
32-
member this.toString() =
33-
toJson this
3428

3529
type [<Pojo>] State = {
3630
state : IState
3731
person : Person Option
3832
}
39-
with
40-
override this.ToString() =
41-
toJson this
42-
43-
member this.toString() =
44-
toJson this

fable/ui.fs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ open Fable.Core
55
open Fable.Core.JsInterop
66
open Fable.Import
77
open Types
8+
open Library
9+
open Fable.PowerPack
810

911
module UI =
12+
13+
let run(what:JS.Promise<'T>) = what |> Promise.iter( ignore )
14+
1015
module R = Fable.Helpers.React
1116
open R.Props
1217

@@ -20,7 +25,15 @@ module UI =
2025
member this.componentDidMount () =
2126
let processedState : State = processState(this.state |> toJson) |> ofJson
2227
printfn "New state %A" processedState
23-
this.setState( processedState )
28+
this.setState( processedState )
29+
30+
match processedState.state with
31+
| IState.Loading ->
32+
promise {
33+
let! person = Library.Impl.loadPerson()
34+
this.setState( { state=IState.Loaded; person=Some(person) } )
35+
} |> run
36+
| _ -> ()
2437

2538
member this.render () =
2639
printfn "Will render state %A" this.state
@@ -56,8 +69,8 @@ module UI =
5669
let content = [header;personEdit;footer] |> List.concat
5770
R.div [] content
5871

59-
let render(person:Person) =
72+
let render() =
6073
ReactDom.render(
6174
R.com<Form,Props, State> {state=IState.Initial} [],
6275
Browser.document.getElementsByClassName("app").[0]
63-
)
76+
)

src/main.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ struct State {
6060
person : Option<Person>
6161
}
6262

63-
fn render_ui(person:&Person) -> Option<String> {
64-
Some(eval_s(&format!("FableLib.Impl.render({})", json!(person).to_string())))
65-
}
66-
67-
68-
fn load_person() -> () {
69-
eval_s("FableLib.Impl.loadPerson()");
63+
fn render_ui() -> String {
64+
eval_s("FableLib.render()")
7065
}
7166

7267
fn parse_person(data : &str) -> Person {
@@ -88,24 +83,17 @@ fn convert_from_js( data : *mut c_char ) -> String {
8883
}
8984
}
9085

91-
#[no_mangle]
92-
pub fn callback( data : *mut c_char ) -> () {
93-
let received = convert_from_js(data);
94-
println!( "Called back from Fable with {:?}", received );
95-
let person = Some(received).map( |p| parse_person(&p) ).unwrap();
96-
println!("loaded {:?}; the person'name is {}",person, person.name);
97-
println!("Fable reports the age to be {}", get_sent_person_age(&person) );
98-
render_ui( &person );
99-
}
100-
10186
#[no_mangle]
10287
pub fn process_state( data : *mut c_char ) -> *mut c_char {
10388
let received = convert_from_js(data);
10489
println!( "Processing...{:?}", received );
10590
let state : State = serde_json::from_str(&received).unwrap();
10691
println!( "Got {:?}", state );
10792
let result =
108-
if state.state == STATE_INITIAL { State { state : STATE_LOADING, .. state } }
93+
if state.state == STATE_INITIAL {
94+
println!("Loading person from Fable...");
95+
State { state : STATE_LOADING, .. state }
96+
}
10997
else { state };
11098
println!( "Will return {:?}", result );
11199
CString::new(json!(result).to_string()).unwrap().into_raw()
@@ -114,8 +102,7 @@ pub fn process_state( data : *mut c_char ) -> *mut c_char {
114102
#[no_mangle]
115103
pub fn app() {
116104
println!("Rust code in main() started...");
117-
println!("Loading person from Fable...");
118-
load_person();
105+
render_ui();
119106
println!("... and we are done!");
120107
}
121108

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var webpack = require("webpack");
33

44
var cfg = {
55
devtool: "source-map",
6-
entry: "./temp/library.js",
6+
entry: "./temp/ui.js",
77
output: {
88
path: path.join(__dirname, "public"),
99
filename: "bundle.js",

0 commit comments

Comments
 (0)