From 83011ea344f7c936213520f935c8e45b1513a4b6 Mon Sep 17 00:00:00 2001 From: Alberto Torres Date: Thu, 5 Sep 2024 16:48:35 +0200 Subject: [PATCH] Add `use_threads` option to `loadUri`. --- libs/loadable/loadable.nim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libs/loadable/loadable.nim b/libs/loadable/loadable.nim index 46c51a0..a5122ba 100644 --- a/libs/loadable/loadable.nim +++ b/libs/loadable/loadable.nim @@ -231,11 +231,14 @@ func escapeUTF8*(s: string): string = else: result &= '%' & c.byte.toHex +# TODO: automatically disable threads when not in main thread + proc loadUri*( uri: string, onload_func: proc(ok: bool, err: string, data: SliceMem[byte]) = nil, range = (-1,-1), auto_start = true, + use_threads = true, ): Fetch {.discardable.} = echo "fetching ", uri @@ -243,7 +246,7 @@ proc loadUri*( var start_func: proc(self: LoadableResource) var self: Fetch var uri = uri - var use_threads = false + var use_threads = use_threads when not defined(onlyLocalFiles): when defined(emscripten): const is_remote = true @@ -253,6 +256,7 @@ proc loadUri*( if is_remote: uri = uri.escapeUTF8 when defined(emscripten): + use_threads = false # API is already threaded start_func = proc(self: LoadableResource) = var attr: emscripten_fetch_attr_t emscripten_fetch_attr_init(attr) @@ -273,7 +277,6 @@ proc loadUri*( self.onload(false, &"Error fetching {fetch.url}: {err_msg}") discard emscripten_fetch(attr, uri.cstring) else: - use_threads = true var client = newHttpClient() var response: string start_func = proc(self: LoadableResource) = @@ -290,6 +293,8 @@ proc loadUri*( )) if not is_remote: + use_threads = false # TODO: detect networked file system? + # TODO: also test if there's better perf in local start_func = proc(self: LoadableResource) = try: var memfile = memfiles.open(uri, mode=fmRead)