johnny-quesada-developer / easy-web-worker

This is a library to easily handle and create web-workers, if you are a web developer and never tried this... do not wait any longer! Workers are an amazing option to improve user experience and make your webpages even greater
MIT License
18 stars 1 forks source link

cancelAll and override (my 2 cents) #2

Closed dianlight closed 1 year ago

dianlight commented 1 year ago

hello, nice work. I've tried your library for an opensource project I'm following and it's very promising.

My 2 cents:

as per specs a terminated worker can't be used anymore so you should dereference it (browser garbage collector thanks you! :) ) and recreate it if you want it to still work.

currently your cancelAll function is final ( a new terminate! ) so override() can't work either.

The correct procedure should be (metacode).

Your code:

  /**
   * Terminates the worker and remove all messages from the queue
   * Execute the cancel callback of each message in the queue if provided
   * @param {unknown} reason - reason why the worker was terminated
   */
  public cancelAll(reason?: unknown): void {
    this.worker?.terminate();

    const messages = Array.from(this.messagesQueue.values());

    messages.forEach((message) => message.cancel(reason));

    this.messagesQueue = new Map();
  }

rif https://developer.mozilla.org/en-US/docs/Web/API/Worker rif https://www.w3schools.com/html/html5_webworkers.asp rif https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webworker rif https://stackoverflow.com/questions/50350316/how-to-restart-web-worker-after-stop

Now I'm not able to contribute but from the end of April onwards if you want I can look at us and send you a PR.

in the meantime that you solve I would recommend you to document the fact that the functions are not working. Like me many might try it and see that it doesn't work (and abandon it!). And it's a shame because in my opinion it's a great work and idea.

Regards L

johnny-quesada-developer commented 1 year ago

Hey @dianlight , Thanks for the feedback, it's super helpful. I totally agree that the issue you pointed out is a problem, and I'm on it. I'll let you know as soon as I have a solution.

I'm excited that you're interested in contributing in the future! Let's definitely keep in touch. If you have any other feedback or ideas, please don't hesitate to share.

Thanks again for your help, I really appreciate it!

johnny-quesada-developer commented 1 year ago

Hi @dianlight ,

Thank you for your feedback. I have made the necessary changes to the library to ensure that the cancelAll, override, and overrideAfterCurrent methods send a cancellation request to the worker, enabling a smooth release of resources. Additionally, these methods now support a new parameter:

@param {boolean} force - If set to true, the worker will be terminated immediately without waiting for a response. This will result in a clean and available worker, ready to perform new tasks.

Furthermore, the issue of the worker being unusable after calling terminate has been resolved.

Thank you again for your valuable feedback and support.

Best regards,