-
Notifications
You must be signed in to change notification settings - Fork 179
Question - Web workers #183
Comments
What exactly you plan for Web Workers? I have Web Services working in mine for App Caching my output files, depending on the use you have for Web Workers things might work. Just in case my repo: Luchillo/Ng2-Pro-webpack-starter, i'm using a plugin named |
I've eventually find out how exactly configure webpack worker loader together with typescript, so this issue can be closed. |
Hi @Sorceror, I'm trying to do the same thing, would you mind posting your working configuration please? |
Configuration is based on private worker: Worker = new (<any>require("worker-loader?name=loaderWorker.js!../workers/loaderWorker")); Based on https://github.com/webpack/webpack/tree/master/examples/web-worker , microsoft/TypeScript#1946 and webpack-contrib/worker-loader@b19200b |
Hi @Sorceror, did you use a separate tsconfig for your web workers? If so, how did you configure your webpack.config? If not, how are you able to use "postMessage" in your web worker? |
Hey @antonmata , I'm not sure what you mean by "separate tsconfig for web workers", I have one
private _myWorker: PromiseWorker = new PromiseWorker(new (<any>require("myworker?name=myWorker.js!../workers/myWorker")));
...
this._myWorker.postMessage(data, "doStuff").then(response => {
...
}); As a response register function is used, which I do import in worker code (e.g. import { register } from '../path-to-libs/promiseWorker/register';
register(message => {
if (message.content) {
return `My response to message from web worker`;
} else
throw `Message does not contain any data to be loaded ${message.toString()}`;
}); I did modify the library a bit when I port it to TS for my project, so I'm sorry if the arguments does not fit 100%, but methods name and the way of usage should be completely same. |
@Sorceror, thanks for the response. Ah I see! I was trying to use postMessage inside the worker. But since the WebWorker typings "lib.webworker.d.ts" was not being loaded by default in Visual Studio Code, I moved the worker files to a different folder with its own tsconfig with the "webworker" lib added to get intellisense:
My next problem was in how to configure webpack to use that tsconfig only for the worker files. However, I could not see a way to configure the "worker-loader" to use a specific tsconfig. So I abandoned the idea and just went with using webpack's multi-compiler; essentially creating 1 webpack config for the workers, and another for the rest. However, after seeing your response, I might try that out. I don't know how I would feel losing proper webworker intellisense (ie. using "window" inside a worker file should throw an error in vscode), but if it means getting rid of the 2nd webpack config, it might be worth it. Thanks for pointing me to "promise-worker". I would have not known about it, hadn't you mentioned it. Thanks! |
@antonmata I'm not sure that I understand what exactly are you trying to achieve. You want to send message from one web worker to another? The sole purpose of the library is to send message to web worker from regular client code and then get answer back in promise manner. I guess you could send the message from one worker to another if you would create the other worker instance in the worker that want to send the message to the other worker :), or if you pass the instance of the other worker to the sender worker somehow.. But if you have only the problem of getting proper |
@Sorceror, Sorry if I wasn't clear. I am just trying to run a simple worker. I am calling
In terms of intellisense/typings, it is that There are actually 2 "global" method signatures for In any case, I already made peace with the solution that I found. Not the most ideal solution, but works. I just wanted to know if I could get worker-loader to play nice with the setup, but alas, I don't think I'll be able to get my cake and eat it too. =P Thank you though for talking about your solution and for putting up with my mindless ramblings. ;) For reference:
|
@antonmata You're using it differently than it was designed :). If you check one of my previous answers, you'll see how to use the code. Create worker somewhere in your main code, something like this private _myWorker: PromiseWorker = new PromiseWorker(new (<any>require("myworker?name=myWorker.js!../workers/myWorker")));
...
this._myWorker.postMessage(data, "doStuff").then(response => {
// here in response is what did you 'return' in the worker
}); So you'll send the message to worker with In the worker though, you have to use import { register } from '../path-to-libs/promiseWorker/register';
register(message => {
if (message.content) {
return `My response to message from web worker`;
} else
throw `Message does not contain any data to be loaded ${message.toString()}`;
}); So instead or using Check the documentation of https://github.com/nolanlawson/promise-worker for further details. |
@Sorceror, Yeah, the main difference is that you are using promise-worker. I opted to just use I think there is a misunderstanding; I was not pertaining to Here is my
And here is my web worker,
|
Thanks @semagarcia for the workaround. |
I wanted to give a quite update - just got this working on TypeScript 2.3 with Webpack 1.14 and worker-loader 0.8 with the following configuration: App.tsx
typings/custom.d.ts
|
There might be slightly more general solution using wildcard character in module names App.tsx import * as MyWorker from "worker-loader!../../worker";
const worker: Worker = new MyWorker(); typings/custom.d.ts declare module "worker-loader!*" {
const content: any;
export = content;
} Notice the I'm not able to test this solution right now, so consider it just as a suggestion. |
Works great, thanks @Sorceror |
@Sorceror
in this configuration |
You didn't post the configuration, but I guess this microsoft/TypeScript#3337 (comment) should resolve your problem. |
Is it possible to use this loader with Web workers? Do I need to configure it somehow?
I've try to find any TypeScript/WebPack related web workers examples, but without any luck.
It's probably naive question, but could someone point me to the right direction or link some existing typescript + webpack example with web workers?
It would help me a lot!
The text was updated successfully, but these errors were encountered: