Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement setInterval? #11

Open
adhamsalama opened this issue Jul 10, 2023 · 2 comments
Open

How to implement setInterval? #11

adhamsalama opened this issue Jul 10, 2023 · 2 comments

Comments

@adhamsalama
Copy link

Hello.

I had so much fun following this tutorial.

I implemented setTimeout using the answer to my question here denoland/deno#19231

#[op]
async fn op_set_timeout(delay: u64) -> Result<(), AnyError> {
    tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await;
    Ok(())
}

This works fine because it does nothing but wait and then return to JavaScript where we can execute the callback.

But how could we implement setInterval?
We need to execute the JavaScript callback after every delay, and also return a timer ID so that JavaScript could cancel it later. I don't get how to do all of that in a single function.

So, could you please help me? Thanks in advance!

@mmastrac
Copy link
Contributor

mmastrac commented Jan 6, 2024

Generally when implementing something like setInterval, you'd want to learn towards providing a stream to JS. You'd provide an async op that picks off the next item from a Stream's next function and just call that repeatedly.

@adhamsalama
Copy link
Author

@mmastrac Do you have any extra resources/documentation on how someone would do that? I would love to read more about this.

I want to try to implement as funcionality as I can, for education purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants