Code Monkey home page Code Monkey logo

bridged-worker's Introduction

BridgedWorker

BridgedWorker is a function that makes communication between worker and main thread simple. It also means you can keep all related code in the same file, which is convenient. The source code is written by Daniel Manson and taken with permission from https://gist.github.com/d1manson/6714892.

API

BuildBridgedWorker = function(workerFunction,workerExportNames,mainExportNames,mainExportHandles)
  • workerFunction is a function, the interior of which will be turned into a string and used as a worker
  • workerExportNames should be an array of string function names available to main
  • mainExportNames should be an array of string function names available to worker
  • mainExportHandles should be an array of the actual functions corresponding to the functions in main for both Names arrays, if the function name ends in an asterisk it means that the last argument passed is going to be an array of ArrayBuffers

The result of all this work is that inside the worker we can call main.SomeMainFunction(thing,otherthing,more,[buffer1,buffer2]) and in main we can call myWorker. SomeWorkerFunction(hello,world,[buffer1,buffer2])

Example

var workerCode = function () {
    "use strict;" //this will become the first line of the worker

    CalculateSomething(a, b, c, d) {
        var v = a + b + c + d; //trivial calculation
        main.DisplayResult(v, "hello");
    }

    CalculateSomethingBig(buff, d) {
        var v = new Uint32Array(buff);
        for (var i = 0; i <= v.length; i++) {
            v[i] /= d;
        }
        main.PlotFraction(v.buffer, "done", 0, 2, "world", [v.buffer]);
        //the buffer is fully transfered to the main thread (google "transferable objects javascript")
    }

    //the BuildBridgedWorker will add some extra code on the end to form the complete worker code
}

var DisplayResult = function (val, str) {
    // do something here
}

var PlotFraction = function (buffer, str1, p1, p2, str2) {
    // do something here
}

var theWorker = BuildBridgedWorker(workerCode, ["CalculateSomething", "CalculateSomethingBig*"], //note asterisk indicating ArrayBuffer transfer
    ["DisplayResult", "PlotFraction*"], [DisplayResult, PlotFraction]);

// Some example inputs
var w = 9,
    x = 100,
    y = 0,
    z = 2;
var v = new Uint32Array(100);

// And this is how you call the functions in the worker...

theWorker.CalculateSomething(w, x, y, z);
theWorker.CalculateSomethingBig(v.buffer, x, [v.buffer]);

// Note that with the CalculateSomethingBig the buffer is transfered to the worker thread (and dissapears on the main thread)

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.