File tree Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -19,9 +19,11 @@ module UI =
19
19
20
20
member this.componentDidMount () =
21
21
let processedState : State = processState( this.state |> toJson) |> ofJson
22
- this.setState( processedState )
22
+ printfn " New state %A " processedState
23
+ this.setState( processedState )
23
24
24
25
member this.render () =
26
+ printfn " Will render state %A " this.state
25
27
let header = [
26
28
R.h1 [] [ R.str " Welcome to JavaScript Interop between F# (Fable) and Rust (through Emscripten) Demo" ]
27
29
R.hr [] []
@@ -39,14 +41,17 @@ module UI =
39
41
]
40
42
else []
41
43
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) ]
50
55
51
56
let content = [ header; personEdit; footer] |> List.concat
52
57
R.div [] content
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ use std::ffi::CStr;
10
10
use libc:: c_char;
11
11
use std:: mem;
12
12
13
+ const STATE_INITIAL : i32 = 0 ;
14
+ const STATE_LOADING : i32 = 1 ;
15
+ const STATE_LOADED : i32 = 2 ;
16
+
13
17
/// Safe rust wrapper for emscripten_run_script_int (basically, JS eval()).
14
18
pub fn eval ( x : & str ) -> i32 {
15
19
let x = CString :: new ( x) . unwrap ( ) ;
@@ -98,7 +102,13 @@ pub fn callback( data : *mut c_char ) -> () {
98
102
pub fn process_state ( data : * mut c_char ) -> * mut c_char {
99
103
let received = convert_from_js ( data) ;
100
104
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 ( )
102
112
}
103
113
104
114
#[ no_mangle]
You can’t perform that action at this time.
0 commit comments