@
@
@xpostman2010-10-04 21:15:19
JavaScript
@xpostman, 2010-10-04 21:15:19

Convenient trading in Javascript?

Greetings.
In fact, it's not really about trading. Yes, there seems to be no “pure” trading in Javascript, but there are also webworkers, I know this, but here I want to raise a question about something else. To begin with, I will clarify what exactly I will mean by “trading”.
A typical task is the following. There is some code (for definiteness, wrapped in the myFunc function) and I want to run this code with a certain call, but without waiting for the completion of this call, go to the next line of code, from where I make this call. Usually this problem is solved like this:

setTimeout( myFunc, 10 );
doSomethingElse();

where myFunc() contains the code I want to execute "in the thread" and doSomethingElse() contains the code I want it to start executing without waiting for myFunc() to complete. And if I need to call the myFunc () function as an object method, I can use the closure and apply (), wrapping it in another anonymous function.
The most mysterious thing about this approach is the second argument to the setTimeout() function. Why did I tell her to execute the code after 10 milliseconds, and not immediately - after 0? People write (comments) that setTimeout(fn, 0) often takes longer than setTimeout(fn, 10). And it also offers an even faster way to “order” the asynchronous execution of a function “right now”, using postMessage.
I decided to experiment a little with the code given there and make it a little more convenient. The idea is that a new thread() method is added to the prototype of the Function object, which runs this function asynchronously and forwards all arguments to it. It turned out something like this:
(function() {
     var threads = [];
     var messageName = "start-thread";

     function thread(fn) {
         threads.push(fn);
         window.postMessage(messageName, "*");
     }

     function startThread(event) {
         if (event.source == window &&
             event.data == messageName) {
             event.stopPropagation();
             if (threads.length> 0) {
                 ( threads.shift() )();
             }
         }
     }

     window.addEventListener("message", startThread, true);

     Function.prototype.thread = function() {
         var args = arguments;
         var me = this;
         thread(
             function() {
                 me.apply( null, args );
             }
         );
     }

 })();


var doSomething = function(a, b) {
    alert( a + b );
}

// вызываем doSomething() асинхронно:
doSomething.thread( 2, 3 );

In general, this is almost an exact copy of the code by reference, plus a new thread method on objects of type Function.
And now the question is attention. For the thread method to be really convenient, you need to be able to use it on object methods. That is, somehow you need to “drag” information about the object in the context of which we accessed the method into the thread function. Using a closure completely ruins all the elegance. I would like to use this method like this:
myObject.myMethod.thread( arg1, arg2 );
and as a result of such a call, the myMethod method should be called asynchronously in the context of the myObject object, that is, it should happen
myObject.myMethod.apply( myObject, [ arg1, arg2 ] );

Is it possible?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavlo Ponomarenko, 2010-10-04
@TheShock

mootools.net/docs/core/Native/Function - Function Method: pass

[
[email protected]><e, 2010-10-04
@barmaley_exe

You can make a wrapper for closures so as not to spoil the code.
Just implement it in the form

myObject.myMethod.thread( arg1, arg2 );
unlikely to succeed
myObject.thread( 'myMethod', arg1, arg2 );

K
kmike, 2010-10-06
@kmike

Actually, in mootools all this is implemented , both with binding and with passing arguments. You can either see how, or just use mootools.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question