wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Typescript support #4

Closed motss closed 6 years ago

motss commented 6 years ago

Any love for the Typescript users?

wilk commented 6 years ago

Yeah, sure! I'll rewrite microjob in Typescript so it will be available for typescripter!

nomeyer commented 6 years ago

Hey @wilk, thanks for this package & being willing to rewrite it in Typescript!

I'm looking for a clarification – is it currently possible to use it with Typescript (as an untyped library), or is it just not possible before the rewrite?

I was playing around with it and I can't seem to get around this error:

ReferenceError: tslib_1 is not defined
    at anonymous (eval at parentPort.on (/app/node_modules/microjob/src/worker.js:10:5), <anonymous>:9:5)
    at __executor__ (eval at parentPort.on (/app/node_modules/microjob/src/worker.js:10:5), <anonymous>:30:3)
    at MessagePort.parentPort.on (/app/node_modules/microjob/src/worker.js:12:27)
    at MessagePort.emit (events.js:182:13)
    at MessagePort.onmessage (internal/worker.js:66:8)

Thanks!

motss commented 6 years ago

@nomeyer I'm a TypeScript user and have tried using it with TypeScript. Do you have a reduced test case?

wilk commented 6 years ago

@nomeyer thank you for using it! Sorry for the delay: microjob has been rewritten in TS in version 0.2.0 🎉

I invite you all to try it and to open new issues and PRs!

nomeyer commented 6 years ago

Hey @wilk, thanks a lot for that release, much appreciated 😁

Unfortunately, I'm still getting the same error (ReferenceError: tslib_1 is not defined) – does it mean anything to you?

FWIW after gradually stripping things out of my worker function I managed to get this to work:

export function doStuff(data: IStuff): Promise<IStuff> {
  console.log(data);
  return Promise.resolve(data);
}

And I get the ReferenceError: tslib_1 is not defined as soon as I add async, like so:

export async function doStuff(data: IStuff): Promise<IStuff> {
  console.log(data);
  return data;
}
wilk commented 6 years ago

Hi @nomeyer !

Thank you for using it and for reporting this issue!

Well, I don't understand why it's giving you that error but it makes sense using an async function instead of a sync one with a Promise.resolve call. However, you should be able to define a doStuff sync function like so:

export function doStuff(data: IStuff): IStuff {
  console.log(data);
  return data;
}

If you think this should be better highlighted inside the docs, then I'll put it asap.

nomeyer commented 6 years ago

Thanks for your quick reply 🙂

If you think this should be better highlighted inside the docs, then I'll put it asap.

No I think the docs are pretty clear that one can use a sync or async function 👍

My jobs are async so I was hoping to do something like this, that's why I'm trying to get async function doStuff to work. I could try to rewrite my worker without async / await but the two examples in my previous example should be equivalent 🤔 I'll see if I can find some time for more experiments.

wilk commented 6 years ago

Ok, thanks, then feel free to open a new issue for the related problem 💪