Open
Description
How can I implement Implement Cancellable or interruptible timeout in javy ?
I am right now polyfilling setTimeout like below -
fn set_timeout_api() -> impl FnMut(&JSContextRef, JSValueRef, &[JSValueRef]) -> Result<JSValue> {
move |context: &JSContextRef, _this: JSValueRef, args: &[JSValueRef]| {
let callback = args.get(0).unwrap();
let default = to_qjs_value(context, &JSValue::Int(0)).unwrap();
let timeout = args.get(1).unwrap_or(&default);
thread::sleep(Duration::from_millis(
timeout
.as_f64()
.expect("Unable to convert timeout to milliseconds") as u64,
));
println!("timeout reached");
if callback.is_function() {
let mut argsvec: Vec<JSValueRef> = vec![];
if args.len() > 2 {
for i in 2..args.len() {
argsvec.push(args.get(i).unwrap().to_owned())
}
}
let res = callback.call(
&to_qjs_value(context.to_owned(), &JSValue::Undefined).unwrap(),
&argsvec,
);
}
Ok(JSValue::Undefined)
}
}
But it is not cancellable it will only be cleared after timeout .