perf: keep heavy work off UI thread

This commit is contained in:
Hakan Seven committed 2026-07-25 18:49:18 +03:00
1 parent 12bf126646
commit deec48ad99
75 files changed
+11702 -518

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
[package]
name = "ocs_web_worker"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
crate-type = ["cdylib"]
[dependencies]
acadrust = { version = "0.4", features = ["serde"] }
bincode = "1.3"
getrandom = { version = "0.3", features = ["wasm_js"] }
js-sys = "0.3"
wasm-bindgen = "0.2"
+32
View File
@@ -0,0 +1,32 @@
use std::io::Cursor;
use acadrust::io::dwg::DwgReader;
use acadrust::DxfReader;
use js_sys::Uint8Array;
use wasm_bindgen::prelude::*;
/// Parse DWG/DXF on a dedicated browser worker and return a compact serialized
/// document. The main wasm instance only deserializes and installs it, so the
/// expensive bit/handle/object decode never occupies the browser UI thread.
#[wasm_bindgen]
pub fn parse_document(name: String, bytes: Uint8Array) -> Result<Uint8Array, JsValue> {
let bytes = bytes.to_vec();
let ext = name.rsplit('.').next().unwrap_or_default().to_lowercase();
let document = match ext.as_str() {
"dwg" => DwgReader::from_stream(Cursor::new(bytes))
.read()
.map_err(|error| JsValue::from_str(&error.to_string()))?,
"dxf" => DxfReader::from_reader(Cursor::new(bytes))
.map_err(|error| JsValue::from_str(&error.to_string()))?
.read()
.map_err(|error| JsValue::from_str(&error.to_string()))?,
_ => {
return Err(JsValue::from_str(&format!(
"Unsupported file format: .{ext}"
)))
}
};
let encoded =
bincode::serialize(&document).map_err(|error| JsValue::from_str(&error.to_string()))?;
Ok(Uint8Array::from(encoded.as_slice()))
}