scala-js/scala-js-dom

ExecutionContext in scala-js-dom ajax methods

MrAIring opened this issue · 10 comments

Noticed that methods for working with ajax in scala-js-dom extensions don't respect any ExecutionContext. Is there any particular reason for that? Or should I make a PR?

Why should they take an ExecutionContext? They're just making a synchronous
call right-now, the future that you get back takes execution contexts for
everything it does =P

On Mon, Nov 10, 2014 at 4:51 PM, Boris Shabalin notifications@github.com
wrote:

Noticed that that methods for working with ajax in scala-js-dom extensions
https://github.com/scala-js/scala-js-dom/blob/master/src/main/scala/org/scalajs/dom/extensions/Extensions.scala#L162
don't respect any ExecutionContext. Is there any particular reason for
that? Or should I make a PR?


Reply to this email directly or view it on GitHub
#62.

According to the documentation: "...They [execution contexts] are essential for the future method because they handle how and when the asynchronous computation is executed." So, future that I getting back will use execution context for methods like flatMap but not for making actual ajax call.

How do you change the threadpool an XMLHttpRequest executes on?

On Mon, Nov 10, 2014 at 5:00 PM, Boris Shabalin notifications@github.com
wrote:

According to documentation
http://docs.scala-lang.org/overviews/core/futures.html "Execution
contexts execute tasks submitted to them, and you can think of execution
contexts as thread pools. They are essential for the future method because
they handle how and when the asynchronous computation is executed." So,
future that I getting back will use execution context for methods like
flatMap but not for making actual ajax call.


Reply to this email directly or view it on GitHub
#62 (comment)
.

There is no threadpools in js environment but we have at least two different execution contexts in scala.js. Even if I import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow my ajax calls will remain asynchronious.

So what is it you are really proposing =P

On Mon, Nov 10, 2014 at 5:40 PM, Boris Shabalin notifications@github.com
wrote:

There is no threadpools in js environment but we have at least two
different execution contexts in scala.js. Even if I import
scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow my ajax
calls will remain asynchronious.


Reply to this email directly or view it on GitHub
#62 (comment)
.

I've taken a look at it, I don't see it actually doing anything =/ from a user point of view, how is this different from the status quo?

With proposed change user can control how ajax calls executed by means of custom ExecutionContext. For example, I may want all of my calls to be synchronous with JSExecutionContext.runNow, or I want to make sure that there is no more than n simultaneous requests to server and make my own execution context for that purpose.

I don't see why the library needs to know about this. If you really wished to wrap your calls in a Future, you can already do so. and flatten the results out. The contents of the Ajax function does not use any Future methods, and thus doesn't need an execution context. Just because you can do it inside the function doesn't mean you should. In fact, if you can just-as-easily do it outside the function, that's a good sign it shouldn't be stuffed inside.

I get your point, thanks.