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

setTimeout or setImmediate shim #29

Open
kennethjor opened this issue Feb 17, 2014 · 8 comments
Open

setTimeout or setImmediate shim #29

kennethjor opened this issue Feb 17, 2014 · 8 comments
Milestone

Comments

@kennethjor
Copy link

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 or setImmediate 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?

@lance
Copy link
Contributor

lance commented Feb 17, 2014

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
[2] https://github.com/vert-x/mod-lang-js/blob/master/src/main/resources/vertx.js#L108

On Feb 16, 2014, at 10:20 PM, Kenneth Jorgensen notifications@github.com wrote:

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 or setImmediate functions out of the box. This would make it immediately compatible with tools which require this, such as Q or async.

I create 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?


Reply to this email directly or view it on GitHub.

@kennethjor
Copy link
Author

@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.

@lance
Copy link
Contributor

lance commented Feb 17, 2014

Are you saying you want these functions available at a global scope, out of the box, without a call to require()? If that's the case, I'm sorry, but I won't be able to accommodate you. Maybe I'm still just misunderstanding what you need.

@kennethjor
Copy link
Author

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.

@lance
Copy link
Contributor

lance commented Feb 17, 2014

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.

@kennethjor
Copy link
Author

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:

org.mozilla.javascript.EcmaError: ReferenceError: "setTimeout" is not defined. (file:/tmp/9341481/lib/underscore.js#685)

And Q:

org.mozilla.javascript.EcmaError: ReferenceError: "setTimeout" is not defined. (file:/tmp/9341481/lib/q.js#201)

@lance
Copy link
Contributor

lance commented Mar 4, 2014

@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 langs.properties file in your application root that looks like this:

dynjs=io.vertx~lang-dynjs~1.0.0-SNAPSHOT:org.dynjs.vertx.DynJSVerticleFactory
.js=dynjs

This should get you going.

@lance lance added this to the 1.1.0 milestone Mar 10, 2014
@tanneess
Copy link

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.

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

3 participants