Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
ng-webworker
Advanced tools
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
###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
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.
// 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);
);
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);
});
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 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.
FAQs
ng-webworker creates dynamic webworkers so angular apps can be multi-threaded.
The npm package ng-webworker receives a total of 277 weekly downloads. As such, ng-webworker popularity was classified as not popular.
We found that ng-webworker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.