Skip to content

Commit 81abfe1

Browse files
Desktop: Remove editor from wasm wrapper (#3023)
* Add a few useful npm scripts for native * Allow compiler to prune editor from wasm wrapper in native builds * Fix warnings * Review cleanup * Fix npm desktop build script names
1 parent 1164359 commit 81abfe1

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

frontend/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
"profiling": "npm run setup && npm run wasm:build-profiling && concurrently -k -n \"VITE,RUST\" \"vite\" \"npm run wasm:watch-profiling\"",
1212
"production": "npm run setup && npm run wasm:build-production && concurrently -k -n \"VITE,RUST\" \"vite\" \"npm run wasm:watch-production\"",
1313
"---------- BUILDS ----------": "",
14+
"build": "npm run wasm:build-production && vite build",
1415
"build-dev": "npm run wasm:build-dev && vite build",
15-
"build-native": "npm run native:build-dev && vite build",
16+
"build-native": "npm run native:build-production && vite build",
17+
"build-native-dev": "npm run native:build-dev && vite build",
1618
"build-profiling": "npm run wasm:build-profiling && vite build",
17-
"build": "npm run wasm:build-production && vite build",
1819
"---------- UTILITIES ----------": "",
1920
"lint": "eslint . && tsc --noEmit",
2021
"lint-fix": "eslint . --fix && tsc --noEmit",
2122
"---------- INTERNAL ----------": "",
2223
"setup": "node package-installer.js",
2324
"native:build-dev": "wasm-pack build ./wasm --dev --target=web --features native",
25+
"native:build-production": "wasm-pack build ./wasm --release --target=web --features native",
2426
"wasm:build-dev": "wasm-pack build ./wasm --dev --target=web",
2527
"wasm:build-profiling": "wasm-pack build ./wasm --profiling --target=web",
2628
"wasm:build-production": "wasm-pack build ./wasm --release --target=web",

frontend/wasm/src/editor_api.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// on the dispatcher messaging system and more complex Rust data types.
66
//
77
use crate::helpers::translate_key;
8-
use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER};
9-
use editor::application::Editor;
8+
use crate::{EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER};
109
use editor::consts::FILE_SAVE_SUFFIX;
1110
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
1211
use editor::messages::input_mapper::utility_types::input_mouse::{EditorMouseState, ScrollDelta, ViewportBounds};
@@ -28,6 +27,11 @@ use wasm_bindgen::JsCast;
2827
use wasm_bindgen::prelude::*;
2928
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, ImageData, window};
3029

30+
#[cfg(not(feature = "native"))]
31+
use crate::EDITOR;
32+
#[cfg(not(feature = "native"))]
33+
use editor::application::Editor;
34+
3135
static IMAGE_DATA_HASH: AtomicU64 = AtomicU64::new(0);
3236

3337
fn calculate_hash<T: std::hash::Hash>(t: &T) -> u64 {
@@ -140,6 +144,7 @@ impl EditorHandle {
140144

141145
#[wasm_bindgen]
142146
impl EditorHandle {
147+
#[cfg(not(feature = "native"))]
143148
#[wasm_bindgen(constructor)]
144149
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
145150
let editor = Editor::new();
@@ -153,6 +158,16 @@ impl EditorHandle {
153158
editor_handle
154159
}
155160

161+
#[cfg(feature = "native")]
162+
#[wasm_bindgen(constructor)]
163+
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
164+
let editor_handle = EditorHandle { frontend_message_handler_callback };
165+
if EDITOR_HANDLE.with(|handle| handle.lock().ok().map(|mut guard| *guard = Some(editor_handle.clone()))).is_none() {
166+
log::error!("Attempted to initialize the editor handle more than once");
167+
}
168+
editor_handle
169+
}
170+
156171
// Sends a message to the dispatcher in the Editor Backend
157172
#[cfg(not(feature = "native"))]
158173
fn dispatch<T: Into<Message>>(&self, message: T) {
@@ -247,6 +262,7 @@ impl EditorHandle {
247262
let g = f.clone();
248263

249264
*g.borrow_mut() = Some(Closure::new(move |_timestamp| {
265+
#[cfg(not(feature = "native"))]
250266
wasm_bindgen_futures::spawn_local(poll_node_graph_evaluation());
251267

252268
if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
@@ -929,6 +945,7 @@ fn set_timeout(f: &Closure<dyn FnMut()>, delay: Duration) {
929945
}
930946

931947
/// Provides access to the `Editor` by calling the given closure with it as an argument.
948+
#[cfg(not(feature = "native"))]
932949
fn editor<T: Default>(callback: impl FnOnce(&mut editor::application::Editor) -> T) -> T {
933950
EDITOR.with(|editor| {
934951
let mut guard = editor.try_lock();
@@ -942,6 +959,7 @@ fn editor<T: Default>(callback: impl FnOnce(&mut editor::application::Editor) ->
942959
}
943960

944961
/// Provides access to the `Editor` and its `EditorHandle` by calling the given closure with them as arguments.
962+
#[cfg(not(feature = "native"))]
945963
pub(crate) fn editor_and_handle(callback: impl FnOnce(&mut Editor, &mut EditorHandle)) {
946964
handle(|editor_handle| {
947965
editor(|editor| {
@@ -964,6 +982,7 @@ pub(crate) fn handle(callback: impl FnOnce(&mut EditorHandle)) {
964982
});
965983
}
966984

985+
#[cfg(not(feature = "native"))]
967986
async fn poll_node_graph_evaluation() {
968987
// Process no further messages after a crash to avoid spamming the console
969988
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {

frontend/wasm/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub static NODE_GRAPH_ERROR_DISPLAYED: AtomicBool = AtomicBool::new(false);
2020
pub static LOGGER: WasmLog = WasmLog;
2121

2222
thread_local! {
23+
#[cfg(not(feature = "native"))]
2324
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
2425
pub static MESSAGE_BUFFER: std::cell::RefCell<Vec<Message>> = const { std::cell::RefCell::new(Vec::new()) };
2526
pub static EDITOR_HANDLE: Mutex<Option<editor_api::EditorHandle>> = const { Mutex::new(None) };
@@ -50,7 +51,7 @@ pub fn panic_hook(info: &panic::PanicHookInfo) {
5051

5152
if !NODE_GRAPH_ERROR_DISPLAYED.load(Ordering::SeqCst) {
5253
NODE_GRAPH_ERROR_DISPLAYED.store(true, Ordering::SeqCst);
53-
editor_api::editor_and_handle(|_, handle| {
54+
editor_api::handle(|handle| {
5455
let error = r#"
5556
<rect x="50%" y="50%" width="600" height="100" transform="translate(-300 -50)" rx="4" fill="var(--color-error-red)" />
5657
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="18" fill="var(--color-2-mildblack)">

frontend/wasm/src/native_communcation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use editor::{application::Editor, messages::prelude::FrontendMessage};
1+
use editor::messages::prelude::FrontendMessage;
22
use js_sys::{ArrayBuffer, Uint8Array};
33
use wasm_bindgen::prelude::*;
44

@@ -9,12 +9,12 @@ pub fn receive_native_message(buffer: ArrayBuffer) {
99
let buffer = Uint8Array::new(buffer.as_ref()).to_vec();
1010
match ron::from_str::<Vec<FrontendMessage>>(str::from_utf8(buffer.as_slice()).unwrap()) {
1111
Ok(messages) => {
12-
let callback = move |_: &mut Editor, handle: &mut EditorHandle| {
12+
let callback = move |handle: &mut EditorHandle| {
1313
for message in messages {
1414
handle.send_frontend_message_to_js_rust_proxy(message);
1515
}
1616
};
17-
editor_api::editor_and_handle(callback);
17+
editor_api::handle(callback);
1818
}
1919
Err(e) => log::error!("Failed to deserialize frontend messages: {e:?}"),
2020
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"scripts": {
55
"---------- DEV SERVER ----------": "",
66
"start": "cd frontend && npm start",
7-
"start-desktop": "cd frontend && npm run build-native && cargo run -p graphite-desktop",
7+
"start-desktop": "cd frontend && npm run build-native-dev && cargo run -p graphite-desktop",
88
"profiling": "cd frontend && npm run profiling",
99
"production": "cd frontend && npm run production",
1010
"---------- BUILDS ----------": "",
1111
"build-dev": "cd frontend && npm run build-dev",
1212
"build-profiling": "cd frontend && npm run build-profiling",
1313
"build": "cd frontend && npm run build",
14+
"build-desktop": "cd frontend && npm run build-native && cargo build -r -p graphite-desktop",
15+
"build-desktop-dev": "cd frontend && npm run build-native-dev && cargo build -p graphite-desktop",
1416
"---------- UTILITIES ----------": "",
1517
"lint": "cd frontend && npm run lint",
1618
"lint-fix": "cd frontend && npm run lint-fix"

0 commit comments

Comments
 (0)