(func: ArgCallable, vm: &VirtualMachine)
| 112 | |
| 113 | #[pyfunction] |
| 114 | fn request_animation_frame(func: ArgCallable, vm: &VirtualMachine) -> PyResult { |
| 115 | use alloc::rc::Rc; |
| 116 | use core::cell::RefCell; |
| 117 | |
| 118 | // this basic setup for request_animation_frame taken from: |
| 119 | // https://rustwasm.github.io/wasm-bindgen/examples/request-animation-frame.html |
| 120 | |
| 121 | let f = Rc::new(RefCell::new(None)); |
| 122 | let g = f.clone(); |
| 123 | |
| 124 | let weak_vm = weak_vm(vm); |
| 125 | |
| 126 | *g.borrow_mut() = Some(Closure::wrap(Box::new(move |time: f64| { |
| 127 | let stored_vm = weak_vm |
| 128 | .upgrade() |
| 129 | .expect("that the vm is valid from inside of request_animation_frame"); |
| 130 | stored_vm.interp.enter(|vm| { |
| 131 | let func = func.clone(); |
| 132 | let args = vec![vm.ctx.new_float(time).into()]; |
| 133 | let _ = func.invoke(args, vm); |
| 134 | |
| 135 | let closure = f.borrow_mut().take(); |
| 136 | drop(closure); |
| 137 | }) |
| 138 | }) as Box<dyn Fn(f64)>)); |
| 139 | |
| 140 | let id = window() |
| 141 | .request_animation_frame(&js_sys::Function::from( |
| 142 | g.borrow().as_ref().unwrap().as_ref().clone(), |
| 143 | )) |
| 144 | .map_err(|err| convert::js_py_typeerror(vm, err))?; |
| 145 | |
| 146 | Ok(vm.ctx.new_int(id).into()) |
| 147 | } |
| 148 | |
| 149 | #[pyfunction] |
| 150 | fn cancel_animation_frame(id: i32, vm: &VirtualMachine) -> PyResult<()> { |
no test coverage detected