Skip to content

Commit fe6dd0a

Browse files
committed
working with the state more
1 parent 78d9073 commit fe6dd0a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

fable/ui.fs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ module UI =
1919

2020
member this.componentDidMount () =
2121
let processedState : State = processState(this.state |> toJson) |> ofJson
22-
this.setState( processedState )
22+
printfn "New state %A" processedState
23+
this.setState( processedState )
2324

2425
member this.render () =
26+
printfn "Will render state %A" this.state
2527
let header = [
2628
R.h1 [] [ R.str "Welcome to JavaScript Interop between F# (Fable) and Rust (through Emscripten) Demo" ]
2729
R.hr [] []
@@ -39,14 +41,17 @@ module UI =
3941
]
4042
else []
4143

42-
let footer = [
43-
R.str (
44-
match this.state with
45-
| Initial -> "Initial"
46-
| Loading -> "Loading"
47-
| Loaded -> "Loaded"
48-
)
49-
]
44+
printf "Application state is %A..." this.state.state
45+
let actualState =
46+
match this.state.state with
47+
| IState.Initial -> "Initial"
48+
| IState.Loading -> "Loading"
49+
| IState.Loaded -> "Loaded"
50+
| _ -> "Unknown"
51+
52+
printfn "... which is %A" actualState
53+
54+
let footer = [ R.str ( actualState) ]
5055

5156
let content = [header;personEdit;footer] |> List.concat
5257
R.div [] content

src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use std::ffi::CStr;
1010
use libc::c_char;
1111
use std::mem;
1212

13+
const STATE_INITIAL: i32 = 0;
14+
const STATE_LOADING: i32 = 1;
15+
const STATE_LOADED: i32 = 2;
16+
1317
/// Safe rust wrapper for emscripten_run_script_int (basically, JS eval()).
1418
pub fn eval(x: &str) -> i32 {
1519
let x = CString::new(x).unwrap();
@@ -98,7 +102,13 @@ pub fn callback( data : *mut c_char ) -> () {
98102
pub fn process_state( data : *mut c_char ) -> *mut c_char {
99103
let received = convert_from_js(data);
100104
println!( "Processing...{:?}", received );
101-
data
105+
let state : State = serde_json::from_str(&received).unwrap();
106+
println!( "Got {:?}", state );
107+
let result =
108+
if state.state == STATE_INITIAL { State { state : STATE_LOADING, .. state } }
109+
else { state };
110+
println!( "Will return {:?}", result );
111+
CString::new(json!(result).to_string()).unwrap().into_raw()
102112
}
103113

104114
#[no_mangle]

0 commit comments

Comments
 (0)