npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-webworker

v0.2.3

Published

ng-webworker creates dynamic webworkers so angular apps can be multi-threaded.

Downloads

1,152

Readme

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

###Installation for Testing

npm install

###Run Tests

grunt karma

###Build

grunt uglify

#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.