rwaldron / jquery-hive

jQuery Plugin for creating and managing web workers across implementations. Includes Hive.Pollen.js - the thread-safe utility library for connecting worker threads to the Hive.
http://weblog.bocoup.com/javascript-web-workers-from-basics-to-jquery-hive-part-i
203 stars 24 forks source link

Problem with send to worker under Firefix but not Chrome #3

Open zsimpson opened 13 years ago

zsimpson commented 13 years ago

The following code executes perfectly under Chrome but acts like the second call to "send" to the worker thread fails under Firefox.

main_thread

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script>
<script src="/javascripts/jquery.hive.js"></script>
<script type="text/javascript">
    var frameComplete = true;

    $(document).ready( function() {
        startThread();
        runOneFrame();
    });

    function startThread() {
        // DELETE the old thread
        // PROBLEM: Acting like this isn't doing anything
        $.Hive.destroy();

        // LAUNCH a new thread
        $.Hive.create({
            count: 1,
            worker: '/javascripts/play_worker_test.js',
            receive: function (data) {
                if( data.cmd == "done" ) {
                    console.log( "recvd frame complete" );
                    frameComplete = true;
                }
            },  
        });

        $.Hive.get(0).send( "go" );

        frameComplete = true;
    }

    function runOneFrame() {
        if( frameComplete ) {
            if( $.Hive.get(0) ) {
                console.log( "sending frame start" );
                $.Hive.get(0).send( "go" );
                frameComplete = false;
            }
        }
        setTimeout( runOneFrame, 1 );
    }

</script>

</html>

worker

importScripts('/javascripts/jquery.hive.pollen.js');

$(function( data ) {
    for( var i=0; i<1000000000; i++ ) {
    }
    $.send( { "cmd":"done" } );
});

rwaldron commented 13 years ago

This is bad news, it seems to just take a long time to respond... ~7 seconds

In Chrome, with timestamps added: http://gyazo.com/93a1c0b8c5e5eab4e0127129270c1b2d.png

In Firefox, with timestamps added: http://gyazo.com/d4e853e7aaa17d089f6ec749edc23303.png

fearphage commented 13 years ago

How about Opera and Safari?

rwaldron commented 13 years ago

Safari uses the same implementation of the Worker constructor as Chrome (its in WebKit). Opera is untested

rwaldron commented 13 years ago

Incidentally, I was wrong in assuming the implementations would perform the same... http://gyazo.com/d2a433aff3d6bee9eca87148c561cb07.png