bytecodealliance / bytecodealliance/javy

Implement Cancellable or interruptible setTimeout in javy

Open
#650 6 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Rust
Stars
2.7k
Forks
135
Avg merge
1d 44m
Merged PRs (30d)
11

Description

How can I implement Implement Cancellable or interruptible timeout in javy ?
I am right now polyfilling setTimeout like below -
```js
fn set_timeout_api() -> impl FnMut(&JSContextRef, JSValueRef, &[JSValueRef]) -> Result {
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 = 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 .

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.