# surface reconstruction in javascript

**URL:** https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434
**Category:** Web
**Created:** [October 17, 2020, 10:02am UTC](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434 "2020-10-17T10:02:25Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [November 2, 2020, 9:24pm UTC](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434/21 "2020-11-02T21:24:07Z")

</div>

After looking at the links you refer to, that it is a bug in Emscripten. Even if they fix the bugs, I am curious if running the wasm code in a new thread, which is required for synchronous fetch, will cause errors with VTK and OpenGL thread safety?

---

<div class="post-metadata">

### Author: ![SArbabi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@SArbabi](https://discourse.vtk.org/u/SArbabi)
#### Post date: [November 2, 2020, 11:15pm UTC](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434/22 "2020-11-02T23:15:22Z")

</div>

right, when asynchronous fetching used, how can I get access to the content of the file in a variable that I want to use? fetch-\>data is accessible in downloadSucceeded, but how can I get access to that in main?

---

<div class="post-metadata">

### Author: ![SArbabi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@SArbabi](https://discourse.vtk.org/u/SArbabi)
#### Post date: [November 2, 2020, 11:18pm UTC](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434/23 "2020-11-02T23:18:22Z")

</div>

regarding to your question, as [API page](https://emscripten.org/docs/api_reference/fetch.html#synchronous-fetches) suggests, I guess synchronous fetch operation is available in both main thread and pthreads with `--proxy-to-worker + -s USE_PTHREADS=1` flags.

---

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [November 3, 2020, 4:04pm UTC](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434/24 "2020-11-03T16:04:57Z")

</div>

I call emsripten\_fetch from a member function of my class (SwisVtkRadarWindow). I assign the reference of SwisVtkRadarWindow to the fetch attribute called “userData” by using the “this” keyword.

> emscripten\_fetch\_attr\_t attr;  
> emscripten\_fetch\_attr\_init(&attr);  
> strcpy(attr.requestMethod, “GET”);  
> attr.attributes = EMSCRIPTEN\_FETCH\_LOAD\_TO\_MEMORY;  
> attr.userData = this;  
> attr.onsuccess = downloadSucceeded;  
> emscripten\_fetch(&attr, path.c\_str());

Then in the downloadSucceeded callback I use a static cast to get my class from userData and process the data.

> void downloadSucceeded(emscripten\_fetch\_t\* fetch) {
> 
> while (fetch-\>readyState != 4)  
> {  
> std::cout \<\< “Waiting For Download To Complete.” \<\< std::endl;  
> }
> 
> std::cout \<\< "Finished downloading " \<\< fetch-\>numBytes \<\< " bytes, and " \<\< fetch-\>totalBytes \<\< " total bytes from URL " \<\< fetch-\>url \<\< std::endl;  
> // The data is now available at fetch-\>data[0] through fetch-\>data[fetch-\>numBytes-1];  
> std::vector data(fetch-\>numBytes, 0);  
> memcpy(data.data(), fetch-\>data, fetch-\>numBytes);  
> emscripten\_fetch\_close(fetch); // Free data associated with the fetch.
> 
> auto radwin = static\_cast\<SwisVtkRadarWindow\*\>(fetch-\>userData);  
> radwin-\>DownloadSuccess(data);
> 
> }

Regarding the Emscripten compiler flags “–proxy-to-worker + -s USE\_PTHREADS=1”. When I tried this I was getting compile errors saying I can’t use --proxy-to-worker flags with USE\_PTHREADS=1, which conflicts with the documentation.

[Previous page](https://discourse.vtk.org/t/surface-reconstruction-in-javascript/4434.md?page=1)
