Git Product home page Git Product logo

easywebworker's Introduction

Easywebworker v0.2.1
Easy Communication Protocol For Web Workers
Just execute worker functions from browser, and main functions from worker.

Also work when web worker is not available. So support Web Worker on older Internet Explorers.

Features

  • Fallback support for old browsers. (AKA Internet Explorer Support.)
  • Execute functions directly. Nested functions are supported.
  • Execute global functions from worker.
  • Start Worker with startup data.
  • Alias for console.log for ease debugging.

Changelog

  • 0.2.1 - XDomainRequest Fallback Support added for crossdomain request on older internet explorers.

Prepare

On Browser

// Just include script to page and create EasyWebWorker with context.
var worker = new EasyWebWorker("worker.js", this);

In Worker

// Just import script.
importScripts("easy-web-worker.js");

Methods

Error Statement (On browser.):

worker.onerror = function(event,filename,lineno,message){
    // ...
}

Terminate From Browser:

worker.terminate();
// or
worker.close();

Terminate In Worker (Same as default.):

self.close();

Examples

Example 1 (Simple Usage)

Browser:

// Execute function
worker.execute("getSquares", [2,3,5]);

// And our callback function.
function getSquaresCallback(event, squares) {
    console.log("Here is our squares: ",squares)
}

Worker:

function getSquares(event, numberArray){
    // Do stuff.
    squares = [];
    for(var i=0, len=numberArray.length; i<len; i++){
        number = numberArray[i];
        squares.push(number*number);
    }

    // Call our callback function.
    self.execute("getSquaresCallback", squares);
}

Example 2 (Nested Functions)

Browser:

// Execute function.
worker.execute("textOperations.reverseText", "Hello guys wazzup?")

// Our nested callback function.
var NestedFunctions = {
  textPrinter: {
      printToConsole: function(event, textToPrint) {
          console.log("Here is our reversed text: ", textToPrint, this)

          // Context test.
          this.printToTitle(textToPrint)
      },
      printToTitle: function(text) {
          window.document.title = text
      }
  }
}

Worker:

// Our nested function.
textOperations = {
  reverseText: function(event, text) {
    var reversedText = text.split("").reverse().join("");
    return self.execute("NestedFunctions.textPrinter.printToConsole", reversedText);
  }
};

Example 3 (Startup Data)

(It uses worker file's querystring. So keep it as tiny as possible.) Browser:

// Create startup data.
var startupData = {name:"Derp", surname:"Derpson", age:"23"}

// Create worker with startupData.
var workerTwo = new EasyWebWorker('demo-worker.js', this, startupData)

// Execute worker.
workerTwo.execute("whatIsTheSettings")

// Get our startupData back.
getSettingsBack = function(event, startupData){
    console.log("Here is our startup data: ", startupData)
}

Worker:

function whatIsTheSettings(){
    // Give settings back.
    self.execute("getSettingsBack", self.startupData)
}

Example 4 (Call Global Function)

Worker:

self.execute("window.console.log", "Hello world!")

// There is an alias for console log.
self.log("Hello World!")

easywebworker's People

Contributors

ramesaliyev avatar

Watchers

 avatar  avatar

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.