iandis / isolated_worker

An isolated worker for Flutter (Isolate) and Web (Web Worker). Behaves almost the same as the compute function, except it is not a one-off worker.
MIT License
42 stars 11 forks source link

document is not supporting inside JS function which called by isolate_worker #7

Closed deepbhavsar closed 2 years ago

deepbhavsar commented 2 years ago

I am creating the element using the document.createElement('input') inside the JS function. I am calling that function using the JsIsolatedWorker().run(functionName: 'alertMessage',arguments: '100') It is giving me error:

This is my JS function. I am getting image using input type=file. Plan is to return file path from this function.

async function createDynamicInput() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
   var file = e.taralertMessageget.files[0];
   console.log(file);
   return file;
}
input.click();
}

Calling this function using:

var result = await JsIsolatedWorker().run(
        functionName: 'createDynamicInput',
        arguments: '100',
      );
      print("==$result");

Error: ReferenceError: document is not defined.

Let me know the correct way or require to make changes into current code. Thanks

iandis commented 2 years ago

Hi, have you imported the script file using JsIsolatedWorker.importScripts first before running the function?

Edit: The main problem actually comes from calling the document since it's not available on Web Workers. reference

iandis commented 2 years ago

Closing this as this is not the package's issue itself.