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

Web Worker Example #5676

Closed
siddjain opened this issue Nov 15, 2015 · 3 comments
Closed

Web Worker Example #5676

siddjain opened this issue Nov 15, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@siddjain
Copy link

Is there any place I can find an example of writing a web worker in typescript? I have created a new file myworker.ts that looks like below:

/// <reference path="IWorkerMessages.ts" />
self.onmessage = (ev: MessageEvent) =>
{
    var data = <IWorkerIncoming>ev.data;


} 

// I get a compiler error while trying to define self.deserialize
self.deserialize = (data: IWorkerIncoming) =>
{

}

could the file be refactored to something like

class MyWorker implements Worker
{
}

Edited by @DanielRosenwasser to add code fences.

@DanielRosenwasser
Copy link
Member

Webworkers in TypeScript work the same as in JavaScript. The only thing in question is where to get the typings from, which is our lib.webworker.d.ts. Take a look at http://blog.scottlogic.com/2015/08/10/ts-vs-proj.html and #4855.

In the future, StackOverflow is a better venue for these types of questions - just as a heads up.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Nov 15, 2015
@siddjain
Copy link
Author

Thanks for the link. Actually I found it more confusing than helpful to be honest. I am not using any virtual projects. Further I am developing a single page app with no MVC. I do my compilation within VS and am not using command line. The problems I am struggling with are:

  • How to compile the web worker code into a separate js
  • Looking at examples of web workers, it seems the compiled js ought to look something like:
    self.onmessage = function()...
    but if I write a typescript file that implements the Worker interface, that is not the compiled js I will get.
    I posted here as I thought response would be faster (and it was, thanks :)

@kitsonk
Copy link
Contributor

kitsonk commented Nov 16, 2015

These sort of questions are probably better asked on Stack Overflow.

@mhegazy mhegazy closed this as completed Feb 20, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants