Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide simple example of addDependency #8

Open
amergin opened this issue Jul 27, 2015 · 7 comments
Open

provide simple example of addDependency #8

amergin opened this issue Jul 27, 2015 · 7 comments

Comments

@amergin
Copy link

amergin commented Jul 27, 2015

How is one supposed to use addDependency to include external libraries? Consider this example:

  • I need to include lodash into my WW
  • I create a service that returns the lodash object:
angular.module('ext.lodash', []).factory('_', function() {
    // WWs don't have access to 'window', will it affect this return value?
    return window._;
});
  • I use addDependency and expect it to be injected:
WorkerService.addDependency('_', 'ext.lodash', 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js');

This later results in an DI error when calling createAngularWorker. What am I doing wrong? I consider adding external libraries one of the most important feature since they allow me to include relevant math and utility libraries.

@FredrikSandell
Copy link
Owner

Hi Amergin,

You are correct. The ability to use DI is one of the most important
features. I will look in to this as soon as possible and try to provide you
with a response shortly.

Br,
Fredrik
Den 27 jul 2015 6:36 em skrev "amergin" notifications@github.com:

How is one supposed to use addDependency to include external libraries?
Consider this example:

  • I need to include lodash into my WW
  • I create a service that returns the lodash object:

angular.module('ext.lodash', []).factory('', function() {
return window.
;
});

  • I use addDependency and expect it to be injected:

WorkerService.addDependency('_', 'ext.lodash', 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js');

This later results in an DI error when calling createAngularWorker. What
am I doing wrong? I consider adding external libraries one of the most
important feature since they allow me to include relevant math and utility
libraries.


Reply to this email directly or view it on GitHub
#8.

@FredrikSandell
Copy link
Owner

Hi Amergin,

I have attempted to recreate your issue with the following code:

WorkerService.setAngularUrl('http://localhost:3001/bower_components/angular/angular.js');
WorkerService.addDependency('lodash', 'ngLodash', 'http://localhost:3001/bower_components/ng-lodash/build/ng-lodash.js');

var workerPromise = WorkerService.createAngularWorker(['input', 'output', 'lodash', function (input, output, _) {
  output.resolve(_.map(input, function(n) { return n * 3; }));
}]);

workerPromise
  .then(function success(angularWorker) {
    return angularWorker.run([1,2,3,4,5]);
  }, function error(reason) {
    console.log('initialization error');
    console.log(reason);
  }).then(function success(result) {
    console.log(result);
  }, function error(reason) {
    console.log(reason);
  }, function notify(update) {
  console.log(update);
});

That code gave me the console output:

[3, 6, 9, 12, 15]

Granted I was using ng-lodash. Which is a lodash angular adaptation. It is still possible that the angular-workers implementation contain a bug relating to this. But I would need your help in finding that bug.

@amergin
Copy link
Author

amergin commented Jul 28, 2015

What I'm trying to say here is that a method to include libraries that are not Angular-dependent would be nice. A library that would be passed as-is to importScripts and available globally, although that may not be the angular way (hence the service approach in my initial post).

Personally I don't feel there's much point in touching ng-lodash when lodash.js is the more actively developed and does not need interacting with Angular. And that is just one example, math.js and numeric.js are great in heavy math calculations.

@MaestroJurko
Copy link

I also agree with @amergin.

@FredrikSandell
Copy link
Owner

OK, this is not so much a bug as a feature request. Including external scripts in the worker code can be done currently by using the importScripts method call. That is what is used to load angular external dependencies in the web worker. But to make external scripts, not written as angular modules injectable in the worker is something that I consider to be beyond the scope of angular-workers (To the best of my knowledge this can not be directly done in angular running in the main thread).

Either use importScripts and import it directly in the worker code, or create a angularjs service wapper script that wraps and exports the desired feature from the external library. That service can then be injected into the worker.

The dependency injection in the angular-worker library is intended to emulate the behavior of angular in the main thread as closely as possible. Adding the ability to inject external scripts extends the existing functionality of angular, which is not the goal of the library.

@MaestroJurko
Copy link

Ok, I managed to edit the library, so now it takes 3 arguments, and the second one is an array of functions that we would like to use inside of blob. If you are interested in the solution, please tell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@MaestroJurko @amergin @FredrikSandell and others