Convenient treding in Javascript?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/code-flow.club/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
5 views
Welcome.


Actually, it's not all about the functionality. Yes, in the Javascript "clean" functionality like as a present, but there are webworkers, I know, but here I want to raise a question about something else. To begin, I will clarify what exactly I meant by "treding".


A typical task is the following. There is some code (to be specific, vypnuty to the function myFunc) and I want to run this code a challenge, but at the same time without waiting for the completion of this call, go to the next line of code where I make this call. Usually this problem is solved like this:

setTimeout( myFunc, 10 ); doSomethingElse();



where myFunc() contains code that I want to do "in thread", and doSomethingElse() contains code that I want it started before the conclusion of myFunc(). But if I need to call the function myFunc() as object method — I can use circuit and apply () by wrapping it in another anonymous function.


Most mysterious in this approach, the second argument of setTimeout(). Why I told her to run code in 10 milliseconds, but not immediately — after 0? People write (comments) that setTimeout(fn, 0) often works longer than setTimeout(fn, 10). And there's also an even quicker way to "order" asynchronous execution of a function "right now" using postMessage.


I decided to experiment a bit with the code and make it a little more convenient. The idea is that the prototype object of the type Function adds a new method thread () which runs this function asynchronously and forwarded her all the arguments. It turned out 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.the 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 ); } // call doSomething() asynchronously: doSomething.thread( 2, 3 );



In General, this is almost an exact copy of the code in the link, plus a new method to thread objects of type Function.


And now attention a question. To thread were really comfortable, you need to be able to use it for methods of objects. That is, somehow in the thread function you need to "push" information about the object in whose context we turned to the method. The use of circuit will completely ruin the elegance. I want to use this method like this:

myObject.myMethod.thread( arg1, arg2 );


and as a result call a method myMethod should be called asynchronously in the context of myObject, that is supposed to happen

myObject.myMethod.apply( myObject, [ arg1, arg2 ] );



Is it possible?
by | 5 views

3 Answers

0 like 0 dislike
mootools.net/docs/core/Native/Function — Function Method: pass
by
0 like 0 dislike
You can make a wrapper for closures so as not to spoil the code.
Only now implemented in the form of
myObject.myMethod.thread( arg1, arg2 );
unlikely to succeed, rather
myObject.thread( 'myMethod', arg1, arg2 );
by
0 like 0 dislike
Sobstvenno, mootools is all implemented, and bindingon and passing arguments. You can either watch, or just use mootools.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
0 like 0 dislike
4 answers
110,608 questions
257,186 answers
0 comments
23,002 users