Skip to content
/ Ozai Public
forked from mako-taco/Ozai

An easy abstraction of Web Workers allowing for basic threading in the browser.

Notifications You must be signed in to change notification settings

xshyamx/Ozai

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Ozai

An easy abstraction of Web Workers allowing for basic threading in the browser. Use this to prevent the gui from locking unecessarily when performing large amounts of computation.

Creating Ozai

To create an Ozai, pass in an object containing all the functions you wish to execute in a separate thread. If you want the function to execute a callback when it finishes, the callback should be the final argument in the argument list.

var ozai = new Ozai({
  superPower: function(a, b, c, callback) {
    callback( Math.pow(a, Math.pow(b, c)) );
  },
  
  reverseString: function(str, callback) {
    var result = str.split('').reverse().join('');
    callback(result, result.length);
  }
});

Using Ozai

Each of your functions will be created inside of the new Ozai object. Call them like so:

ozai.superPower(2,4,8, function(result) {
  alert("This was calculated in a worker thread: " + result);
});

ozai.reverseString("abcdefghijklmnopqrstuvwzyz", function(result, len) {
  alert(result + " is " + len + " characters long.");
});

Multithreading

To take advantage of multi-core computers, create multiple Ozai instances

var ozais = [];
for(var i=0; i<8; i++) {
  ozais.push(
     new Ozai({
        someFunction: function(a,b,c,cb) {
           ...
        },
        otherFunction: function(x,y,cb) {
           ...
        }
     }));
     
  var ozais[i].someFunction('a', 'b', 'c', function(some, results) {
     console.log(some, results);
  });
}

About

An easy abstraction of Web Workers allowing for basic threading in the browser.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published