-
Notifications
You must be signed in to change notification settings - Fork 13
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
setTimeout or setImmediate shim #29
Comments
Hi Kenneth Do vertx/timers[1] and vertx.runOnContext not give you what you need? [1] https://github.com/vert-x/mod-lang-js/blob/master/src/main/resources/vertx/timer.js On Feb 16, 2014, at 10:20 PM, Kenneth Jorgensen notifications@github.com wrote:
|
@lance They do, but the problem is that when including libraries such as Q and async, they will try and detect which engine they're running in and attach themselves accordingly. Not having any of those hooks available make it difficult to use those libraries. |
Are you saying you want these functions available at a global scope, out of the box, without a call to |
That would be the gist of what I'm suggesting yes (shock horror 😄). I get why you'd be reluctant to add anything to the global scope, but pretty much any JS engine I've ever used provides this functionality. Many libraries depend on them, including libraries like underscore, which have become more or less standard. If not, is there a recommended workaround for getting these to work without constructing custom versions of them? Is there a way I could inject stuff into the global scope of a require call? I can't be the first one to run into this problem. |
I understand. You can pretty easily set up these global objects yourself at the top level of your application before you require a library like Q or underscore, like so: https://gist.github.com/lance/9057460 There is an auxiliary project https://github.com/projectodd/nodyn that does expose the node.js functions globally out of the box. It's still not release ready yet, but getting close. You may want to give that a try as well. |
Thanks for the suggestions. I've had a quick go at nodyn, but you're right it doesn't seem production ready. Nice initiative though, I'll keep it on my radar. I've tried the code you provided in the gist, and made async play nice. However, I cannot make underscore and Q work. I've made a new gist here: https://gist.github.com/kennethjor/9341481 This is the output I get when trying underscore:
And Q:
|
@kennethjor It appears that the Rhino implementation does not scope its runtime in a way that allows you to create top-level global variables in your application script. If you use DynJS as your runtime, your script will work. To do this, add a
This should get you going. |
You can find an implementation of setTimeout, setInterval, ... relying on the Vertx event loop (so it runs in the same thread as other code which is important for Nashorn) here: https://gist.github.com/tanneess/8c4581c53f19083fff2346d759fc83f2 Note: this works for me with es6-promises node package. |
I'm sure this is not the first time it's come up, but I would like to see the Vert.x JavaScript module support support some form of the
setTimeout
orsetImmediate
functions out of the box. This would make it immediately compatible with tools which require this, such as Q or async.I created a pull request for Q, but kriskowal suggest adding support for one of these functions in Vert.x itself, and to be honest I agree. I make extensive use for both async and Q throughout my code, and I've had to use custom versions of both in order to make them work. Would it be possible to just add a shim around
vertx.runOnContext
or similar? If it already exists in the code, can we enable it by default?The text was updated successfully, but these errors were encountered: