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

how to access outside function from hamsters.run #37

Closed
darkworks opened this issue Jul 7, 2017 · 4 comments
Closed

how to access outside function from hamsters.run #37

darkworks opened this issue Jul 7, 2017 · 4 comments

Comments

@darkworks
Copy link

hi am processing canvas large data so to speedup things am trying to use hamsters for the first time

so my question is that my outside functions are not accessible within hamsters.run
example

this.testfun = function(val){
 // do something and return 
};

   hamsters.run(params, function() {
     // my code calling function  testfun  but it giver error undefined.  
    }, function(output) {
      console.log(output);
    }, hamsters.maxThreads, true);

thanks

@austinksmith
Copy link
Owner

@darkworks Thanks for using the library however please in the future please follow the provided issue template when submitting tickets, it makes it much easier for me to keep track of them down the road.

The issue you are facing is that threads are sandboxed environments so you cannot share scope with the main thread, I know this is counter intuitive at first but it's actually a good thing in the sense of thread safety and ensuring parallel tasks don't conflict with one another. This will change somewhat down the road as JavaScript adds support for Atomic operations for now though you have a few options to accomplish what you want here.

You can either do the following and simply make your function testFun and modify the inside of testFun to make use of the internal params object.

this.testfun = function(val) {
 // do something and return using params and rtn.data 
};

hamsters.run(params, testFun, function(output) {
  console.log(output);
}, hamsters.maxThreads, true);

Or you can just define testFun inside your hamsters function.

hamsters.run(params, function() {
 var testfun = function(val) {
   // do something and return
 };
 rtn.data.push(testFun(value));
}, function(output) {
  console.log(output);
}, hamsters.maxThreads, true);

@austinksmith
Copy link
Owner

Take a look at the logic from lzwFloss for an example of how you can define functions to be used within hamsters.

https://github.com/austinksmith/lzwFloss.js/blob/master/lzwFloss.js

@darkworks
Copy link
Author

darkworks commented Jul 8, 2017

thanks :)
hmmm i used this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

to pass function body in text in paramas and it worked :)

example :

 var params = {
addfun : 'return a+b',
};

    hamsters.run(params, function() {
var addfun = new Function('a','b',params.addfun);
// now addfun can be used inside.
};

@austinksmith
Copy link
Owner

austinksmith commented Jul 8, 2017

Closing this issue, thanks @darkworks

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

No branches or pull requests

2 participants