GithubHelp home page GithubHelp logo

frontrunner.js's Introduction

FrontRunner.js

FrontRunner is a lightweight (only 874 bytes minified) wrapper on HTML5 Web Workers that implements a switching system and a number of other features.

Using FrontRunner lets you send and receive specific messages between a Worker and your script using familiar syntax.

FrontRunner also lets you spawn a worker by simply passing it a function, allowing you to write multi-threaded, client-side Javascript all in the same script.

##Usage

Include FrontRunner.js in your main HTML file, like you would any other client-side Javascript library.

        <script src="/FrontRunner.js"></script>

####Basic Usage

myscript.js

        //create a new worker pointing at your worker file
        var fr = FrontRunner("myworker.js"); 

        //send a "foo" message to the worker
        fr.send("foo", {"bar": "baz"}); 

        //listen for "qux" messages from worker
        fr.on("qux", function(data){
            doSomething(data);
        })

myworker.js

        //you must import FrontRunner to use it in a seperate worker file
        importScripts('FrontRunner.js'); 

        //create a Worker instance of FrontRunner
        var worker = FrontRunner();

        //listen for "foo" messages from script
        worker.on("foo", function(data){
            doSomething(data);
            //send a "qux" message to your script
            worker.send("qux", data);
        })

####Passing a Function

You can pass a function to FrontRunner instead of a script location. The contents of that function will be used to create the Worker, allowing you to have your code for both threads in the same script.

It is important to note that the function you pass will be entirely in it's own scope.

You do not need to import or instantiate FrontRunner in your worker function. It is made available for you as an argument.

        //create a new worker with a function

        var fr = FrontRunner(function(worker){

            worker.on("foo", function(data){
                doSomething(data);
                worker.send("bar", data);
            })

        })

        fr.send("foo", something);

        fr.on("bar", function(data){
            doSomething(data);
        })

####All Methods

######FrontRunner.send(event, data)

event : string
data: can be any datatype, including blobs

Sends a message between the Worker and the Script,containing data for listeners listening for that event.

######FrontRunner.on(event, callback)

event : string
callback: function(data)

Listens for specific message events sent between the Worker and the Script.

######FrontRunner.remove(event)

event : string

Stop listening for that event.

######FrontRunner.terminate()

Kill the worker thread. Can be called from the Script or the Worker.

##TODO

Support SharedWorkers and ServiceWorkers

Test Suite Makefile

More Tests

##Test The test suite is a simple express app running mocha and selenium.

To run the tests:

    cd test
    npm install
    java -jar selenium-server-standalone-2.46.0.jar
    npm start
    npm test

frontrunner.js's People

Contributors

scoin avatar

Stargazers

 avatar  avatar Jean Christophe Roy avatar Gabriel avatar  avatar Miles Zimmerman avatar  avatar Connor Hammond avatar km avatar Benjamin Mukasa avatar Armen Vartan avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

evaluation-alex

frontrunner.js's Issues

Any way to tell if a worker is started, running, stopped or terminated ?

Any way to tell if a worker is started, running, stopped or terminated ?
besides sending messages from the worker with it's status, something like:

var fr = FrontRunner("frontwork.js");

fr.send("foo", {"bar": "baz"}); 
fr.on("qux", function(data){
	console.log(data)
})

if(fr.isrunning()) {
 //do something
}

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.