Switch from RPC to IPC

This commit is contained in:
Christoph Grabo committed 2022-02-13 20:34:26 +01:00
1 parent afa5a301c7
commit 594a794f05
3 files changed
+59 -35

No files matched your search

+31 -9
View File
@@ -1,5 +1,4 @@
//! Convert a serialized event to an event Trigger //! Convert a serialized event to an event trigger
//!
use std::any::Any; use std::any::Any;
use std::sync::Arc; use std::sync::Arc;
@@ -7,27 +6,50 @@ use std::sync::Arc;
use dioxus_core::{ElementId, EventPriority, UserEvent}; use dioxus_core::{ElementId, EventPriority, UserEvent};
use dioxus_html::on::*; use dioxus_html::on::*;
#[derive(serde::Serialize, serde::Deserialize)]
pub(crate) struct IpcMessage {
method: String,
params: serde_json::Value,
}
impl IpcMessage {
pub(crate) fn method(&self) -> &str {
self.method.as_str()
}
pub(crate) fn params(self) -> serde_json::Value {
self.params
}
}
pub(crate) fn parse_ipc_message(payload: &str) -> Option<IpcMessage> {
let mm = serde_json::from_str(payload);
match mm {
Ok(message) => Some(message),
Err(e) => {
log::error!("could not parse IPC message, error: {e}");
None
}
}
}
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
struct ImEvent { struct ImEvent {
event: String, event: String,
mounted_dom_id: u64, mounted_dom_id: u64,
// scope: u64,
contents: serde_json::Value, contents: serde_json::Value,
} }
pub fn trigger_from_serialized(val: serde_json::Value) -> UserEvent { pub fn trigger_from_serialized(val: serde_json::Value) -> UserEvent {
let ims: Vec<ImEvent> = serde_json::from_value(val).unwrap();
let ImEvent { let ImEvent {
event, event,
mounted_dom_id, mounted_dom_id,
contents, contents,
} = ims.into_iter().next().unwrap(); } = serde_json::from_value(val).unwrap();
// let scope_id = ScopeId(scope as usize);
let mounted_dom_id = Some(ElementId(mounted_dom_id as usize)); let mounted_dom_id = Some(ElementId(mounted_dom_id as usize));
let name = event_name_from_typ(&event); let name = event_name_from_type(&event);
let event = make_synthetic_event(&event, contents); let event = make_synthetic_event(&event, contents);
UserEvent { UserEvent {
@@ -105,7 +127,7 @@ fn make_synthetic_event(name: &str, val: serde_json::Value) -> Arc<dyn Any + Sen
} }
} }
fn event_name_from_typ(typ: &str) -> &'static str { fn event_name_from_type(typ: &str) -> &'static str {
match typ { match typ {
"copy" => "copy", "copy" => "copy",
"cut" => "cut", "cut" => "cut",
+20 -22
View File
@@ -132,23 +132,24 @@ pub fn launch_with_props<P: 'static + Send>(
.with_transparent(cfg.window.window.transparent) .with_transparent(cfg.window.window.transparent)
.with_url("dioxus://index.html/") .with_url("dioxus://index.html/")
.unwrap() .unwrap()
.with_rpc_handler(move |_window: &Window, req: RpcRequest| { .with_ipc_handler(move |_window: &Window, payload: String| {
match req.method.as_str() { parse_ipc_message(&payload)
"user_event" => { .map(|message| match message.method() {
let event = events::trigger_from_serialized(req.params.unwrap()); "user_event" => {
log::trace!("User event: {:?}", event); let event = trigger_from_serialized(message.params());
sender.unbounded_send(SchedulerMsg::Event(event)).unwrap(); log::trace!("User event: {:?}", event);
} sender.unbounded_send(SchedulerMsg::Event(event)).unwrap();
"initialize" => { }
is_ready.store(true, std::sync::atomic::Ordering::Relaxed); "initialize" => {
let _ = proxy.send_event(UserWindowEvent::Update); is_ready.store(true, std::sync::atomic::Ordering::Relaxed);
} let _ = proxy
"browser_open" => { .send_event(user_window_events::UserWindowEvent::Update);
println!("browser_open"); }
let data = req.params.unwrap(); "browser_open" => {
log::trace!("Open browser: {:?}", data); println!("browser_open");
if let Some(arr) = data.as_array() { let data = message.params();
if let Some(temp) = arr[0].as_object() { log::trace!("Open browser: {:?}", data);
if let Some(temp) = data.as_object() {
if temp.contains_key("href") { if temp.contains_key("href") {
let url = temp.get("href").unwrap().as_str().unwrap(); let url = temp.get("href").unwrap().as_str().unwrap();
if let Err(e) = webbrowser::open(url) { if let Err(e) = webbrowser::open(url) {
@@ -157,11 +158,8 @@ pub fn launch_with_props<P: 'static + Send>(
} }
} }
} }
} _ => (),
_ => {} })
}
None
})
.with_custom_protocol(String::from("dioxus"), move |request| { .with_custom_protocol(String::from("dioxus"), move |request| {
// Any content that that uses the `dioxus://` scheme will be shuttled through this handler as a "special case" // Any content that that uses the `dioxus://` scheme will be shuttled through this handler as a "special case"
// For now, we only serve two pieces of content which get included as bytes into the final binary. // For now, we only serve two pieces of content which get included as bytes into the final binary.
+8 -4
View File
@@ -2,7 +2,7 @@ export function main() {
let root = window.document.getElementById("main"); let root = window.document.getElementById("main");
if (root != null) { if (root != null) {
window.interpreter = new Interpreter(root); window.interpreter = new Interpreter(root);
window.rpc.call("initialize"); window.ipc.postMessage(serializeIpcMessage("initialize"))
} }
} }
export class Interpreter { export class Interpreter {
@@ -207,7 +207,7 @@ export class Interpreter {
event.preventDefault(); event.preventDefault();
const href = target.getAttribute("href"); const href = target.getAttribute("href");
if (href !== "" && href !== null && href !== undefined) { if (href !== "" && href !== null && href !== undefined) {
window.rpc.call("browser_open", { href }); window.ipc.postMessage(serializeIpcMessage("browser_open", { href }))
} }
} }
} }
@@ -261,11 +261,12 @@ export class Interpreter {
if (realId == null) { if (realId == null) {
return; return;
} }
window.rpc.call("user_event", { window.ipc.postMessage(serializeIpcMessage(
"user_event", {
event: edit.event_name, event: edit.event_name,
mounted_dom_id: parseInt(realId), mounted_dom_id: parseInt(realId),
contents: contents, contents: contents,
}); }));
} }
}; };
this.NewEventListener(edit.event_name, edit.root, handler); this.NewEventListener(edit.event_name, edit.root, handler);
@@ -544,6 +545,9 @@ export function serialize_event(event) {
} }
} }
} }
function serializeIpcMessage(method, params = {}) {
return JSON.stringify({ method, params });
}
const bool_attrs = { const bool_attrs = {
allowfullscreen: true, allowfullscreen: true,
allowpaymentrequest: true, allowpaymentrequest: true,