|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc overview |
| 5 | + * @name fullGridWebworkersApp |
| 6 | + * @description |
| 7 | + * # fullGridWebworkersApp |
| 8 | + * |
| 9 | + * Main module of the application. |
| 10 | + */ |
| 11 | +var app = angular |
| 12 | + .module('angular-workers-example', ['FredrikSandell.worker-pool']) |
| 13 | + .run(function (WorkerService) { |
| 14 | + //WorkerService.setAngularUrl('../bower_components/angular/angular.js'); |
| 15 | + WorkerService.setAngularUrl('https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js'); |
| 16 | + //WorkerService.addDependency(serviceName, moduleName, url); |
| 17 | + }); |
| 18 | + |
| 19 | +app.controller("myChartCtrl", function($scope,WorkerService) { |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + $scope.awesomeThings = ['HTML5 Boilerplate', 'AngularJS', 'Karma']; |
| 24 | + $scope.data = {}; |
| 25 | + |
| 26 | + $scope.data.reply1 = 'a'; |
| 27 | + $scope.data.reply2 = 'b'; |
| 28 | + $scope.data.reply3 = 'c'; |
| 29 | + |
| 30 | + $scope.test = function (arg) { |
| 31 | + |
| 32 | + /** |
| 33 | + // This contains the worker body. |
| 34 | + // The function must be self contained. |
| 35 | + // The function body will be converted to source and passed to the worker. |
| 36 | + // The input parameter is what will be passed to the worker when it is executed. It must be a serializable object. |
| 37 | + // The output parameter is a promise and is what the worker will return to the main thread. |
| 38 | + // All communication from the worker to the main thread is performed by resolving, rejecting or notifying the output promise. |
| 39 | + // We may optionally depend on other angular services. These services can be used just as in the main thread. |
| 40 | + // But be aware that no state changes in the angular services in the worker are propagates to the main thread. Workers run in fully isolated contexts. |
| 41 | + // All communication must be performed through the output parameter. |
| 42 | + */ |
| 43 | + var workerPromise = WorkerService.createAngularWorker(['input', 'output', '$http', function (input, output, $http) { |
| 44 | + |
| 45 | + var i=0; |
| 46 | + |
| 47 | + var callback = function(i) { |
| 48 | + output.notify(i); |
| 49 | + i++; |
| 50 | + }; |
| 51 | + |
| 52 | + |
| 53 | + //for (var i = 0; i < 10; i++) { callback(i); } |
| 54 | + //var intervalID = setInterval(callback(i), 3000); |
| 55 | + setInterval(function(){ callback(++i); }, Math.floor((Math.random() * 1000) + 100)); |
| 56 | + |
| 57 | + //output.resolve(true); |
| 58 | + //output.reject(false); |
| 59 | + |
| 60 | + }]); |
| 61 | + |
| 62 | + workerPromise |
| 63 | + .then(function success(angularWorker) { |
| 64 | + //The input must be serializable |
| 65 | + return angularWorker.run($scope.awesomeThings); |
| 66 | + }, function error(reason) { |
| 67 | + |
| 68 | + console.log('callback error'); |
| 69 | + console.log(reason); |
| 70 | + |
| 71 | + //for some reason the worker failed to initialize |
| 72 | + //not all browsers support the HTML5 tech that is required, see below. |
| 73 | + }).then(function success(result) { |
| 74 | + |
| 75 | + console.log('success'); |
| 76 | + console.log(result); |
| 77 | + |
| 78 | + //handle result |
| 79 | + }, function error(reason) { |
| 80 | + //handle error |
| 81 | + console.log('error'); |
| 82 | + console.log(reason); |
| 83 | + |
| 84 | + }, function notify(update) { |
| 85 | + //handle update |
| 86 | + |
| 87 | + $scope.data['reply' + arg] += update + '\n'; |
| 88 | + //console.log(arg); |
| 89 | + //console.log(update); |
| 90 | + }); |
| 91 | + |
| 92 | + }; |
| 93 | +}); |
0 commit comments