I have some existing code(running in the browser), that follows a certain interface, which I do not want to break/refactor.
There's a sequence of function calls. One of those functions, which isn't under my control(comes from some library), performs heavy calculations, that freeze the main thread, rendering the UI useless for about 40 seconds.
My idea is to use a WebWorker of course. Problem is, that from the examples i see- a WebWorker requires its own isolated script, that communicates with the main thread via "messages". Refactoring my code to fit this "scheme", would be tedious. I'll give a simplified example to what I want to achieve:
class MySimplifiedFacade{
consturctor(){
...
}
someLightMethod(params){
//Do something light
}
//THIS IS THE FUNCTION I WOULD LIKE TO SOMEHOW ISOLATE INTO A WEBWORKER
veryHeavyCalculations(params){
someExternalLibrary.heavySynchronousCalculation(params)
}
}
The only thing in the sequence of actions, that actually requires a WebWorker, is this one function called "veryHeavyCalculations" in the example.
Is it possible to somehow isolate a specific "piece of code" inside a WebWorker? Problem is, according to my understanding, is that any thing I pass to a WebWorker via messages, must be "serializable", whereas in my case I would need to pass a reference to some existing object.
Any ideas?