Skip to content

Commit 9af1d9a

Browse files
committed
Additional WASM translation layer cleanup
1 parent 21c17cc commit 9af1d9a

File tree

5 files changed

+34
-74
lines changed

5 files changed

+34
-74
lines changed

frontend/src/utilities/response-handler.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export function registerResponseHandler(responseType: ResponseType, callback: Re
3636
state.responseMap[responseType] = callback;
3737
}
3838

39-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4039
export function handleResponse(responseType: string, responseData: any) {
4140
const callback = state.responseMap[responseType];
4241
const data = parseResponse(responseType, responseData);
@@ -50,7 +49,6 @@ export function handleResponse(responseType: string, responseData: any) {
5049
}
5150
}
5251

53-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5452
function parseResponse(responseType: string, data: any): Response {
5553
switch (responseType) {
5654
case "DocumentChanged":

frontend/wasm/src/document.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::dispatch;
12
use crate::shims::Error;
23
use crate::wrappers::{translate_key, translate_tool, Color};
34
use editor::input::input_preprocessor::ModifierKeys;
@@ -9,20 +10,14 @@ use editor::LayerId;
910
use graphene::layers::BlendMode;
1011
use wasm_bindgen::prelude::*;
1112

12-
fn convert_error(err: editor::EditorError) -> JsValue {
13-
Error::new(&err.to_string()).into()
14-
}
15-
16-
fn dispatch<T: Into<Message>>(message: T) {
17-
let messages = crate::EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
18-
crate::handle_responses(messages);
19-
}
20-
2113
/// Modify the currently selected tool in the document state store
2214
#[wasm_bindgen]
2315
pub fn select_tool(tool: String) -> Result<(), JsValue> {
2416
match translate_tool(&tool) {
25-
Some(tool) => Ok(dispatch(ToolMessage::ActivateTool(tool))),
17+
Some(tool) => {
18+
dispatch(ToolMessage::ActivateTool(tool));
19+
Ok(())
20+
}
2621
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
2722
}
2823
}
@@ -32,7 +27,10 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
3227
pub fn set_tool_options(tool: String, options: &JsValue) -> Result<(), JsValue> {
3328
match options.into_serde::<ToolOptions>() {
3429
Ok(options) => match translate_tool(&tool) {
35-
Some(tool) => Ok(dispatch(ToolMessage::SetToolOptions(tool, options))),
30+
Some(tool) => {
31+
dispatch(ToolMessage::SetToolOptions(tool, options));
32+
Ok(())
33+
}
3634
None => Err(Error::new(&format!("Couldn't set options for {} because it was not recognized as a valid tool", tool)).into()),
3735
},
3836
Err(err) => Err(Error::new(&format!("Invalid JSON for ToolOptions: {}", err)).into()),
@@ -53,7 +51,10 @@ pub fn send_tool_message(tool: String, message: &JsValue) -> Result<(), JsValue>
5351
None => Err(Error::new(&format!("Couldn't send message for {} because it was not recognized as a valid tool", tool)).into()),
5452
};
5553
match tool_message {
56-
Ok(tool_message) => Ok(dispatch(tool_message)),
54+
Ok(tool_message) => {
55+
dispatch(tool_message);
56+
Ok(())
57+
}
5758
Err(err) => Err(err),
5859
}
5960
}
@@ -256,10 +257,11 @@ pub fn set_blend_mode_for_selected_layers(blend_mode_svg_style_name: String) ->
256257
"saturation" => BlendMode::Saturation,
257258
"color" => BlendMode::Color,
258259
"luminosity" => BlendMode::Luminosity,
259-
_ => return Err(convert_error(EditorError::Misc("UnknownBlendMode".to_string()))),
260+
_ => return Err(Error::new(&EditorError::Misc("UnknownBlendMode".to_string()).to_string()).into()),
260261
};
261262

262-
Ok(dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode)))
263+
dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode));
264+
Ok(())
263265
}
264266

265267
/// Set the opacity for the selected layers

frontend/wasm/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
pub mod document;
22
mod shims;
33
pub mod utils;
4-
pub mod window;
54
pub mod wrappers;
65

76
use editor::{message_prelude::*, Editor};
87
use std::cell::RefCell;
98
use utils::WasmLog;
109
use wasm_bindgen::prelude::*;
1110

12-
// the thread_local macro provides a way to initialize static variables with non-constant functions
11+
// The thread_local macro provides a way to initialize static variables with non-constant functions
1312
thread_local! {
1413
pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new());
1514
}
@@ -22,24 +21,27 @@ pub fn init() {
2221
log::set_max_level(log::LevelFilter::Debug);
2322
}
2423

25-
pub fn handle_responses(responses: Vec<FrontendMessage>) {
26-
for response in responses.into_iter() {
27-
handle_response(response)
24+
// Sends FrontendMessages to JavaScript
25+
pub fn dispatch<T: Into<Message>>(message: T) {
26+
let messages = EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
27+
28+
for message in messages.into_iter() {
29+
let message_type = message.to_discriminant().local_name();
30+
let message_data = JsValue::from_serde(&message).expect("Failed to serialize response");
31+
32+
let _ = handleResponse(message_type, message_data).map_err(|error| {
33+
log::error!(
34+
"While handling FrontendMessage \"{:?}\", JavaScript threw an error: {:?}",
35+
message.to_discriminant().local_name(),
36+
error
37+
)
38+
});
2839
}
2940
}
3041

42+
// The JavaScript function to call into
3143
#[wasm_bindgen(module = "/../src/utilities/response-handler-binding.ts")]
3244
extern "C" {
3345
#[wasm_bindgen(catch)]
3446
fn handleResponse(responseType: String, responseData: JsValue) -> Result<(), JsValue>;
3547
}
36-
37-
fn handle_response(response: FrontendMessage) {
38-
let response_type = response.to_discriminant().local_name();
39-
send_response(response_type, response);
40-
}
41-
42-
fn send_response(response_type: String, response_data: FrontendMessage) {
43-
let response_data = JsValue::from_serde(&response_data).expect("Failed to serialize response");
44-
let _ = handleResponse(response_type, response_data).map_err(|error| log::error!("javascript threw an error: {:?}", error));
45-
}

frontend/wasm/src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ impl log::Log for WasmLog {
4141
let msg = &format!("%c{}\t{}", name, record.args());
4242
log(msg, color)
4343
}
44+
4445
fn flush(&self) {}
4546
}

frontend/wasm/src/window.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)