///|
extern "js" fn ffi_request_animation_frame(f : () -> Unit) -> Unit =
#|(f) => {
#| if (globalThis.requestAnimationFrame) {
#| requestAnimationFrame(f)
#| } else {
#| setTimeout(f, 0) // fallback
#| }
#|}
///|
pub async fn request_animation_frame(f : () -> Unit noraise) -> Unit noraise {
suspend(fn(resume_ok, _resume_err) {
ffi_request_animation_frame(() => {
f()
resume_ok(())
})
}) catch {
_ => ()
}
}
///|
test "raf mainloop" {
let mut f : (async () -> Unit noraise)? = None
let mut cnt = 0
f = Some(fn() {
let _ = request_animation_frame(() => match f {
Some(next) => {
println("Animation frame \{cnt + 1}")
if cnt >= 5 {
f = None
println("Completed 5 animation frames.")
return
}
cnt += 1
run_async(next)
}
None => ()
})
})
run_async(() => match f {
Some(f) => f()
None => ()
})
}✦ $ moon test --target js
Animation frame 1
Animation frame 2
Animation frame 3
Animation frame 4
Animation frame 5
Animation frame 6
Completed 5 animation frames.
test dom/raf.mbt:24 ("js") failed: TypeError: Cannot read properties of undefined (reading '$tag')
at mizchi$jslib$dom$$moonbit_test_driver_internal_do_execute
target/js/debug/test/dom/dom.internal_test.js
in moonbit_test_driver_internal_do_execute()
const on_err = (err) => {
let e;
_L$2: {
let e$2;
_L$3: {
switch (err.$tag) { // <= this line
case 0: {
const _Failure = err;
const _e = _Failure._0;