blob: 34dd477fc44aad5098ad0e573ee32af689221ff9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
use crate::game::Game;
pub fn run(game: Game, update_period: i32) {
game.render();
let f = Rc::new(RefCell::new(None));
let g = f.clone();
*g.borrow_mut() = Some(Closure::wrap(Box::new(move |_| {
game.update();
game.render();
set_timeout(f.borrow().as_ref().unwrap(), update_period);
}) as Box<dyn FnMut(i32)>));
set_timeout(g.borrow().as_ref().unwrap(), update_period);
}
fn set_timeout(f: &Closure<dyn FnMut(i32)>, timeout: i32) {
web_sys::window().unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(f.as_ref().unchecked_ref(), timeout)
.unwrap();
}
|