-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
Uncaught TypeError: importScripts.map is not a function(…) #36
Comments
Hi @pkerpedjiev. Considering the |
Yup, absolutely:
|
Hmm, seems like the documentation suggests a different usage then there is implemented... 👉 worker.spec.js#L230 |
Try passing |
Thanks for the fast responses! I just tried doing this:
But now I get this error:
The script is definitely present at |
You could try |
Same error :-/ It doesn't seem to be related to your package, so don't worry about it. I'll google around and see what I can come up with. Edit: @andywer, This Stack Overflow question implies that it doesn't work because the script path is taken relative to the blob path. Could this be the problem here as well? Here's a simplified example: index.html: <html>
<body>
<script src="https://cdn.rawgit.com/andywer/threads.js/v0.7.2/dist/threads.browser.js"></script>
Hi there
<script>
var pool = new thread.Pool(1);
pool.run(function(input, done) {
console.log('hi');
console.log(addOne(input));
}, ['worker.js'])
.on('done', function(job, message) {
console.log('done', message);
})
.on('error', function(job, error) {
console.log('error', error);
})
.send(1);
</script>
</body>
</html> worker.js: function addOne(x) {
return x+1;
} |
Can you reproduce the simplified example above @andywer ? Do you think it can be fixed as in the Stack Overflow question or is it a deeper issue? |
Hey @pkerpedjiev. Yes, I was able to reproduce it: http://www.webpackbin.com/4k8GdX9Mz The fix mentioned in the SO post should also work when incorporated into threads.js. Just got to find some time to do so... |
(Need to click on "Log" (in the header) to see the output) |
Just recognized: The Stack Overflow solution is only going to work if you serve the worker script file and provide a link to it (like when using the IE fallback). That's actually not so elegant... :-/ Still thinking... |
Oh, as opposed to bundling the worker script as its own blob? That would probably be ideal but might be tricky because my worker.js first needs to be generated by the build process (e.g. es6 transpilation and import resolution) |
Yeah, having to adapt your webpack config for every piece of code you want to run in a thread pretty much destroys the simplicity gained by using this library... 🙈 Maybe it would be best to automatically create absolute URLs for the |
Yup. That seems to work fine. 👍 Here's the code: var pool = new thread.Pool(1);
var scriptPath = document.location.href;
pool.run(function(input, done) {
importScripts(input.scriptPath + '/worker.js');
console.log('hi');
console.log(addOne(input.val));
})
.on('done', function(job, message) {
console.log('done', message);
})
.on('error', function(job, error) {
console.log('error', error);
})
.send({scriptPath: scriptPath, val: 1}); |
Then there is at least a solution in sight! I hope to find some time this week to provide a fix you can test :) |
Guess this means I am doomed because I need to use an npm module and I am getting this error. @andywer @pkerpedjiev |
Ahh, sorry guys 🙈 I will look into it. Was so busy with other stuff lately... @cozzbie Can you provide a short example of your "I need to use an npm module" situation? |
Definitely. I am trying to use the keypair npm lib to create a public and private set and its crashing on the devices its running on so yeah...threading. const thread = threads.spawn(function (input, done) {
done(keypair());
}, { keypair: "keypair" });
thread.send()
.on('message', function (response) {
console.log("Hope full pairs", response);
thread.kill();
})
.on('error', function (error) {
console.error('Worker errored:', error);
})
.on('exit', function () {
console.log('Worker has been terminated.');
}); The error is the same as the one reported above. @andywer |
@cozzbie God, this documentation is sketchy 🙈🙈 Yeah, I think what you are trying to do does not yet work this way... You want to do something like in the ThreadPool example, right? I wonder why it is documented there, since there is a part in the README where it says that there is no such thing yet... :-/ |
@andrakis Any news on this one? The ability to use an npm module within a worker would be very helpful! Basically the same as the pool example, but in a browser. |
I'm not sure what I have to do with this discussion...my worker implementation is entirely Node.js side, not browser-side. |
A workaround is to create an absolute path for the worker from the current script's path:
|
@pkerpedjiev what is the format of your |
What do you mean? It's a javascript file :) It needs to be a standalone script. So if you have import statements, those need to be resolved using webpack before the script gets called from the worker. |
thanks i got it working... for anyone else here looking for help another workaround is to pass in cdn links for any external deps. Either in your own dedicated worker file via
|
v1.0 beta is here with a whole new API. It will make your life much easier, esp. when Closing. |
I'm trying to use
threads.js
do some some processing but am encountering this error:The workflow goes like this. I first import
Pool
from thethreads
module:And then call it as in the documentation:
The function
getTilesetInfo
is defined in another javascript file calledworker.js
. Is this a valid issue, or am I simply doing something wrong?The text was updated successfully, but these errors were encountered: