Coder Social home page Coder Social logo

ng-webworker's Introduction

#ng-webworker Build Status demo and more instructions: http://mattslocum.github.io/ng-webworker/

###Installation

    # npm
    npm install ng-webworker --save
    # bower
    bower install ng-webworker --save

###Installation for Testing

npm install

###Run Tests

npm test

###Build

npm run build

#Using ng-webworker

Basic Usage

###Include the module

angular.module('demo', ['ngWebworker'])
    .controller('demoCtrl', function($scope, Webworker) {});

###Create a basic worker

// function that will become a worker
function doubler(num) {
    // the return value becomes the resolve of the promise
    return num * 2;
}

var myWorker = Webworker.create(doubler);

###call the worker function

myWorker.run($scope.value).then(function(result) {
    alert("Answer: " + result);
});

##Create an advanced worker

Async (notification) promises

Lets say you want the notification support for webworkers for things like progress bars. There are times you do not want the return value to resolve the function. Maybe you are doing api requests or some other async tasks. An api of two functions is injected into the web worker. If an error is thrown, it will reject.

  • complete - This will resolve the promise
  • notify - Send a notification of data via the promise
// function that will become a worker
function async(first, second) {
    // api to send a promise notification
    notify(first);
    // api to resolve the promise. Note: according to the $q spec, 
    // a promise cannot be used once it has been resolved or rejected.
    complete(second);
}

// mark this worker as one that supports async notifications
var myWorker = Webworker.create(async, {async: true });

// uses the native $q style notification: https://docs.angularjs.org/api/ng/service/$q
myWorker.run(1, 2).then(function(result) {
    // promise is resolved.
    alert('done');
}, null, function(progress) {
    // promise has a notification
    console.log(progress);
);

Extra config

Global config

angular.module('ngWebworker').config(function(WebworkerProvider) {
    WebworkerProvider.setHelperPath("/base/src/worker_wrapper.js");
    WebworkerProvider.setUseHelper(false);
    // transfer ownership doesn't work with the worker_wrapper helper
    WebworkerProvider.setTransferOwnership(true);
});

Instance Config

If you want callback style functions on top of the promise or as an alternative style, you can pass callbacks into the config block. These callbacks only work if async is true. When async is false it uses basic resolves when the function returns.

var myWorker = Webworker.create(async, {
    async: true, // prevent the function return from resolving the promise
    useHelper: true/false, // defaults to false for most browsers. defaults to true for IE.
    onMessage: function(event) {}, // every event from the worker fires this when async:true
    onError: function(event) {}, // error event from the worker
    onReturn: function(data) {}, // return value from the function
    onComplete: function(data) {}, // data from complete/resolve function
    onNotice: function(data) {} // data from notice function
});

IE workarounds

IE strikes again. The way ng-webworker can take a function and turn it into a webworker is by transforming your function into a Blob and executing that blob in a web worker like you would an independant file. Unfortunatly, IE treats blobs as cross domain. The solution is to have a worker shell file that is loaded as a separate file. Your function is strigified and then messaged over to the worker file and evaled to make it behave just like the blobs did.

ng-webworker's People

Contributors

mattslocum avatar flekschas avatar amilajack avatar jirkachadima avatar ivanovjaroslaw avatar

Watchers

James Cloos avatar raj 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.