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

[feature request] Timers should be usable in Node.js environment #176

Closed
teohhanhui opened this issue Dec 3, 2021 · 5 comments · Fixed by #185
Closed

[feature request] Timers should be usable in Node.js environment #176

teohhanhui opened this issue Dec 3, 2021 · 5 comments · Fixed by #185
Labels
bug Something isn't working

Comments

@teohhanhui
Copy link

teohhanhui commented Dec 3, 2021

Describe the Bug

Trying to use timers while running in Node.js environment currently panics with:

panic!("Only supported in a browser or web worker");

Steps to Reproduce

N/A

Expected Behavior

Timers work in Node.js environment.

Actual Behavior

Panic with error message: Only supported in a browser or web worker

Additional Context

Similar to #106

@teohhanhui teohhanhui added the bug Something isn't working label Dec 3, 2021
@teohhanhui teohhanhui changed the title [feature request] Timers should be usable in NodeJS environment [feature request] Timers should be usable in Node.js environment Dec 3, 2021
@teohhanhui
Copy link
Author

Methods such as set_timeout_with_callback_and_timeout_and_arguments_0 are only available from web_sys, which understandably does not support Node.js...

@PhilippGackstatter
Copy link
Contributor

I would also like to have timers working in node.js because it is one of the blocking issues that prevents the libp2p crate from running in node.js. I'm prepared to work on this and was wondering if a fix would be accepted into the crate? I imagine I would need to use js_sys to call node.js's setTimeout function, but haven't gone deeper into the topic yet. Maybe @hamza1311 can comment on whether such a fix would be accepted and any pointers to get started? Thanks very much!

@ranile
Copy link
Collaborator

ranile commented Jan 12, 2022

Assuming that setTimeout and setInterval are available in the global scope, we can use wasm-bindgen to import and use them.

#[wasm_bindgen]
extern {
    #[wasm_bindgen(...)]
    fn set_timeout(...) -> ...;

    #[wasm_bindgen(...)]
    fn set_interval(...) -> ...
}

See relevant docs

Feel free to PR this, @PhilippGackstatter. I would be happy to merge it

@PhilippGackstatter
Copy link
Contributor

Hey @hamza1311, would it be possible to publish a new gloo-timers version relatively soon, perhaps within the next week or so? We'd like to have this available on crates.io. Thanks!

@ranile
Copy link
Collaborator

ranile commented Jan 28, 2022

@PhilippGackstatter just published a new patch version with this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants