Tengo un trabajador web ejecutándose en un ciclo while(true) , el código es demasiado profundo para cambiarlo en este momento. El trabajador puede acceder a un búfer de matriz compartida , tengo un medio para señalar que hay datos presentes en él (en este momento solo se está usando con cadenas, enteros y demás). Quiero pasar un objeto de archivo local a través del búfer de matriz compartida. ¿Cómo puedo serializar el objeto de archivo y deserializarlo en el otro extremo (incluso me parece bien reconstruir un nuevo objeto de archivo en el otro lado si es necesario)?
Esta es mi estructura aproximada, pero agrega un par de miles de líneas y estás ahí :)
<script type='text/javascript'> var s_curFile; var s_curFile2; var sab = new SharedArrayBuffer(65563); //I will probably make this bigger... function JSprocessFilePicker( input ) { let url = input.value; let ext = url.substring( url.lastIndexOf( '.' ) + 1 ).toLowerCase(); if ( input.files && input.files[0] && ( ext == "txt" ) ) { s_curFile = input.files[0]; //I know I can create another file object like this, can i pass just this info in the shared array buffer, so I can create a new file in the worker. //s_curFile2 = new File([s_curFile],s_curFile.name,{type:s_curFile.type}); //how to serialize s_curFile or needed info here! } } var input = document.createElement( "input" ); input.setAttribute( "id", "file_picker" ); input.setAttribute( "type", "file" ); input.setAttribute( "accept", ".txt" ); input.setAttribute( "onchange", "JSprocessFilePicker(this)" ); var worker = new Worker('worker.js'); //I can't do another post message because worker enters a while(true loop) after getting the shared array buffer during startup. And i have to wait for the user to open the file to pass it to the worker try { worker.postMessage({ 'cmd' : 'data', 'sb' : sab }); } catch(e) { alert('Can\'t spawn files to worker - '+e) } </script> <button onclick="input.click();">Open</button>O si hay alguna otra forma que no implique que se procese una devolución de llamada (a menos que haya una forma de procesar manualmente las devoluciones de llamada de mensajes) donde puedo pasar el archivo al trabajador sin postMessage, ¿también sería aceptable para mí?