MCPcopy Index your code
hub / github.com/RustPython/RustPython / request_animation_frame

Function request_animation_frame

crates/wasm/src/browser_module.rs:114–147  ·  view source on GitHub ↗
(func: ArgCallable, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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<()> {

Callers 1

gen_cbFunction · 0.90

Calls 15

newFunction · 0.85
weak_vmFunction · 0.85
windowFunction · 0.85
js_py_typeerrorFunction · 0.85
borrow_mutMethod · 0.80
SomeClass · 0.50
wrapFunction · 0.50
cloneMethod · 0.45
upgradeMethod · 0.45
enterMethod · 0.45
invokeMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected