-
Notifications
You must be signed in to change notification settings - Fork 154
Add node.js support for timers #185
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
Add node.js support for timers #185
Conversation
@hamza1311 would you be able to take a look at this and give feedback? Thanks! |
@PhilippGackstatter can you just pull the functions from global scope? That way we can avoid checking the platform. Also a more human usable name for imported functions would be nice since (unlike web sys) this isn't autogenerated code |
@hamza1311 Done. Using global scope for all platforms now, so it should work for browsers (window, web workers) as well as nodejs (and deno if wasm-bindgen has support for it, not sure). I've previously used the long names so I wouldn't have to rewrite the macro, but that's also fixed now. |
@hamza1311 Actually, I'm not sure whether the |
I don't think it is required. Test pass which means it works. I'm split between testing for node in CI because not all of our crates work on node. I guess that's for a different PR. I'll merge this. It'll be part of the next release |
Unfortunately it seems it's not actually working?
|
@teohhanhui Can you pin down what might be causing the error? I can't tell what's happening from stack trace. This might be related to #187 |
I'm not sure how to debug this. Should I open another issue so we can follow up on this? Or join in on #187? |
@teohhanhui I apologize for not commenting earlier, but I only saw your problem now. It's not much, but if you use |
@ctm:
I cannot test with 0.2.2 as Node.js support was only added in 0.2.3 |
Aha. Makes sense. I wasn't going to venture a guess when I mistakenly thought you could test 0.2.2, but since you can't, my guess is it's not the same as #187 (but it could be very much related), because your backtrace has the death coming from I'm a bit out of my depth here. I know little about web stuff in general and almost nothing about node. However, from my naive perspective, the question I have is whether |
@teohhanhui: I've quickly hacked in a call to
Perhaps it will work for node, but I don't know how to test it. If you give it a try and it works for node, I'll be happy to turn it into a proper pull-request. If it doesn't work for node, at least we have one more data point. |
@ctm Sorry for getting back so late, but it doesn't seem to work. Running
with this tree: https://github.com/teohhanhui/callbag-rs/tree/f7f385a389dea5f54a98c85c2490465939120412
I've verified that this is in my local
and I'm using the latest Is there any way I could possibly help to debug this? |
Thanks, @teohhanhui. Now that I have a failing test I can take a look and see if there's anything I can do. I don't want to get your hopes up though, this is not an area I am particularly knowledgable about. Additionally, today (and the next couple of days) are pretty busy for me. I am, however, fairly persistent and do want a solution that works for everyone, so with both of us pulling in the same direction, we'll probably get one eventually. |
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global by binding to the global `setInterval` and `clearInterval` functions directly. This uses the same approach as used by gloo-timers implemented in [rustwasm/gloo#185](rustwasm/gloo#185) and [rustwasm/gloo#283](rustwasm/gloo#283) given the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)). Co-authored-by: sisou <[email protected]>
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by binding to the global `setInterval` and `clearInterval` functions directly. This uses the same approach used by gloo-timers implemented in [rustwasm/gloo#185](rustwasm/gloo#185) and [rustwasm/gloo#283](rustwasm/gloo#283) given the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)). Co-authored-by: sisou <[email protected]>
Add support for node.js in
gloo-timers
. The motivation is to enable rust-libp2p's usage in node.js.This adds an enum variant to the
WindowOrWorker
struct. An alternative would be to remove that enum entirely and bind to the globalsetTimeout
,setInterval
,clearTimeout
andclearInterval
functions directly. From my understanding, they should be available in window, web workers and node.js (as well as deno). For now, I've stuck to the enum variant. Let me know what you prefer.The node.js detection logic is akin to how the
browser-or-node
npm package does it. What might also be possible is to assume node.js if no window or worker is available.fixes #176
I have tested it locally in a
libp2p
example project.cargo test
still succeeds locally, too. I'm happy to add dedicated node.js tests as well, if deemed useful, but I'm not sure how I would do that.