Use serde to serialize responses (#96)

This commit is contained in:
TrueDoctor
2021-05-02 22:46:47 +02:00
committed by Keavon Chambers
parent f10a3c63d1
commit f8ebff033d
13 changed files with 99 additions and 45 deletions

View File

@@ -328,12 +328,13 @@ export default defineComponent({
},
},
mounted() {
registerResponseHandler(ResponseType["Tool::UpdateCanvas"], (responseData) => {
this.viewportSvg = responseData;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerResponseHandler(ResponseType["Tool::UpdateCanvas"], (responseData: any) => {
this.viewportSvg = responseData.Tool.UpdateCanvas.document;
});
registerResponseHandler(ResponseType["Tool::SetActiveTool"], (responseData) => {
const [activeTool] = responseData;
this.activeTool = activeTool;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerResponseHandler(ResponseType["Tool::SetActiveTool"], (responseData: any) => {
this.activeTool = responseData.Tool.SetActiveTool.tool_name;
});
window.addEventListener("keyup", (e: KeyboardEvent) => this.keyUp(e));

View File

@@ -23,7 +23,8 @@ export function registerResponseHandler(responseType: ResponseType, callback: Re
window.responseMap[responseType] = callback;
}
export function handleResponse(responseType: ResponseType, responseData: string) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function handleResponse(responseType: ResponseType, responseData: any) {
const callback = window.responseMap[responseType];
if (callback) {

View File

@@ -18,8 +18,9 @@ default = ["console_error_panic_hook"]
[dependencies]
console_error_panic_hook = { version = "0.1.6", optional = true }
editor-core = { path = "../../../core/editor", package = "graphite-editor-core" }
wasm-bindgen = "0.2.73"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2.73", features = ["serde-serialize"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.22"

View File

@@ -4,13 +4,11 @@ pub mod utils;
pub mod window;
pub mod wrappers;
use editor_core::{
events::{DocumentResponse, Response, ToolResponse},
Editor, LayerId,
};
use editor_core::{events::Response, Editor};
use std::cell::RefCell;
use utils::WasmLog;
use wasm_bindgen::prelude::*;
use wrappers::WasmResponse;
// the thread_local macro provides a way to initialize static variables with non-constant functions
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
@@ -23,37 +21,19 @@ pub fn init() {
log::set_max_level(log::LevelFilter::Debug);
}
fn path_to_string(path: Vec<LayerId>) -> String {
path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(",")
}
fn handle_response(response: Response) {
let response_type = response.to_string();
match response {
Response::Document(doc) => match doc {
DocumentResponse::ExpandFolder { path, children } => {
let children = children
.iter()
.map(|c| format!("name:{},visible:{},type:{}", c.name, c.visible, c.layer_type))
.collect::<Vec<String>>()
.join(";");
send_response(response_type, &[path_to_string(path), children])
}
DocumentResponse::CollapseFolder { path } => send_response(response_type, &[path_to_string(path)]),
DocumentResponse::DocumentChanged => log::error!("Wasm wrapper got request to update the document"),
},
Response::Tool(ToolResponse::UpdateCanvas { document }) => send_response(response_type, &[document]),
Response::Tool(ToolResponse::SetActiveTool { tool_name }) => send_response(response_type, &[tool_name]),
}
send_response(response_type, response);
}
fn send_response(response_type: String, response_data: &[String]) {
let data = response_data.iter().map(JsValue::from).collect();
handleResponse(response_type, data);
fn send_response(response_type: String, response_data: Response) {
let response_data = JsValue::from_serde(&WasmResponse::new(response_data)).expect("Failed to serialize response");
handleResponse(response_type, response_data);
}
#[wasm_bindgen(module = "/../src/response-handler.ts")]
extern "C" {
fn handleResponse(responseType: String, responseData: Vec<JsValue>);
fn handleResponse(responseType: String, responseData: JsValue);
}
#[wasm_bindgen]

View File

@@ -2,6 +2,8 @@ use crate::shims::Error;
use editor_core::events;
use editor_core::tools::{SelectAppendMode, ToolType};
use editor_core::Color as InnerColor;
use events::Response;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
@@ -24,6 +26,15 @@ impl Color {
}
}
#[derive(Serialize, Deserialize)]
pub struct WasmResponse(Response);
impl WasmResponse {
pub fn new(response: Response) -> Self {
Self(response)
}
}
macro_rules! match_string_to_enum {
(match ($e:expr) {$($var:ident),* $(,)?}) => {
match $e {