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

Is it possible to use this library with web workers? #257

Closed
harrybiddle opened this issue Feb 24, 2020 · 3 comments
Closed

Is it possible to use this library with web workers? #257

harrybiddle opened this issue Feb 24, 2020 · 3 comments

Comments

@harrybiddle
Copy link

Hello! Thank you for the library, it's a very nice API and the documentation is fantastic.

After using the library I realised that since my async task is entirely CPU, promises aren't appropriate and I would need to use web workers instead.

Is library a good fit for web workers, or should I look somewhere else? Does anybody have a code snippet where they use react-async with web workers?

Thank you

@ghengeveld
Copy link
Member

ghengeveld commented Feb 24, 2020

Hi Harry,

Happy you ask, because I actually developed React WebWorker before building React Async. However it does not provide a hook and has less features in general.

We actually have plans for a future version of React Async to support subscriptions, which would allow it to be used with a Web Worker. There's work-in-progress which supports subscriptions, but can't find the time to wrap that up into something shippable.

@harrybiddle
Copy link
Author

That looks great, I'll give it a go :). I am using create-react-app and since posting I also experimented with workerize-loader. I'm not sure if it's best practice but the code snippet below seems to work:

// eslint-disable-next-line import/no-webpack-loader-syntax
import worker from "workerize-loader!../../worker";

function dispatch({ myArg }) {
  let inst = worker();
  return inst.myFunction(myArg);
}

export default function MyComponent() {
  myArg= ... ;
  const { data, error, isPending } = useAsync({
    promiseFn: dispatch,
    myArg: myArg
  });
  ...
}

@harrybiddle
Copy link
Author

Hey guys - don't want to clutter up your open issues, and my question is answered and problem resolved. Hopefully this thread is useful for others who come across it - closing out

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

No branches or pull requests

2 participants