Skip to content

Commit 50fdc10

Browse files
author
Dima Korolev
committed
The two-way callback.
1 parent 935c2bb commit 50fdc10

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

step09_wasm/code/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ use wasm_bindgen::prelude::*;
22

33
#[wasm_bindgen]
44
extern "C" {
5-
fn js_callback(msg: &str);
5+
fn js_log(msg: &str);
6+
fn js_return_another_name() -> String;
67
}
78

89
#[wasm_bindgen]
910
pub fn greet(name: &str) -> String {
10-
js_callback(&format!("Callback from Rust, says {name}."));
11+
js_log(&format!("Callback from Rust, says {name}."));
12+
let another_name = js_return_another_name();
13+
js_log(&format!("Another callback from Rust, says {another_name}."));
1114
format!("Wasm works, says {name}.")
1215
}

step09_wasm/code/web/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import init, { greet } from "./pkg/wasm.js";
22

3-
window.js_callback = (msg) => {
3+
window.js_log = (msg) => {
44
console.log("Called the callback from Rust:", msg);
55
};
66

7+
window.js_return_another_name = () => {
8+
return "Max";
9+
};
10+
711
init().then(() => {
812
document.getElementById("out").innerText = greet("Dima");
913
});

0 commit comments

Comments
 (0)