Skip to content

Implement Cancellable or interruptible setTimeout in javy #650

Description

@redoC-A2k

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 .

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions