Skip to content

Commit 82363c7

Browse files
committed
Initial pass at an interval-based tick closure
1 parent fad68a4 commit 82363c7

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

bot_template.js.j2

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ function decode(encoded) {
2121
return bytes.buffer;
2222
}
2323

24-
let wasm_module;
25-
__wbg_init(decode(wasm_data)).then(result => wasm_module = result);
24+
__wbg_init(decode(wasm_data)).then(result => result.run());

gaussian-bots/Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gaussian-bots/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ opt-level = 'z'
1515
[dependencies]
1616
js-sys = "0.3.67"
1717
wasm-bindgen = "0.2.90"
18+
web-sys = { version = "0.3.67", features = ["Window"] }
19+
20+
[package.metadata.wasm-pack.profile.release]
21+
wasm-opt = ['-O4']
1822

1923
[package.metadata.wasm-pack.profile.release.wasm-bindgen]
20-
omit-default-module-path = true
24+
omit-default-module-path = true

gaussian-bots/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
pub mod wrappers;
22

33
use wasm_bindgen::prelude::*;
4+
use web_sys;
5+
6+
#[wasm_bindgen]
7+
pub fn run() -> Result<(), JsValue> {
8+
// todo: set up setInterval call
9+
let window = web_sys::window().expect("Error: window not found");
10+
let tick_fn = Closure::<dyn Fn()>::new(move || tick());
11+
window.set_interval_with_callback_and_timeout_and_arguments_0(
12+
tick_fn.as_ref().unchecked_ref(),
13+
5000,
14+
)?;
15+
16+
tick_fn.forget(); // memory leak, but we can't drop the reference and still call periodically.
17+
Ok(())
18+
}
19+
20+
#[wasm_bindgen]
21+
pub fn tick() {
22+
wrappers::al_log("Hi");
23+
}
424

525
#[wasm_bindgen]
626
pub fn refill_resources() {

gaussian-bots/src/wrappers.rs

+4
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ pub fn use_skill_targetless(skill: Skill) {
2525
juse_skill(skill_to_str(skill), JsValue::NULL, JsValue::NULL);
2626
jlog("Used an hp or mp pot... FROM RUST! 🦀");
2727
}
28+
29+
pub fn al_log(s: &str) {
30+
jlog(s);
31+
}

0 commit comments

Comments
 (0)