get bridge working!
This commit is contained in:
parent
242fd2510f
commit
178f3190ce
3 changed files with 58 additions and 116 deletions
|
@ -2,6 +2,9 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use crate::manifest::AppPermission;
|
||||
|
||||
/**
|
||||
Actions are what the dashboard receives and app sends on `window.parent`
|
||||
*/
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[serde(tag = "type", content = "payload")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
@ -16,7 +19,8 @@ pub enum Action {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PayloadRequestPermissions {
|
||||
pub permissions: Vec<AppPermission>,
|
||||
pub redirect_path: String,
|
||||
// #[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub redirect_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||
|
|
|
@ -1,67 +1,11 @@
|
|||
use crate::{locales::LocaleCode, manifest::AppPermission};
|
||||
use crate::manifest::{AppPermission, LocaleCode};
|
||||
|
||||
use super::ThemeType;
|
||||
// use bus::{Bus, BusReader};
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use strum_macros::EnumIter;
|
||||
// use web_sys::js_sys::Object;
|
||||
|
||||
// pub struct EventChannels {
|
||||
// pub handshake: Bus<PayloadHanshake>,
|
||||
// pub response: Bus<PayloadResponse>,
|
||||
// pub redirect: Bus<PayloadRedirect>,
|
||||
// pub theme: Bus<PayloadTheme>,
|
||||
// pub locale_changed: Bus<PayloadLocaleChanged>,
|
||||
// pub token_refreshed: Bus<PayloadTokenRefreshed>,
|
||||
// }
|
||||
//
|
||||
// impl EventChannels {
|
||||
// pub fn new() -> Self {
|
||||
// Self {
|
||||
// handshake: Bus::new(10),
|
||||
// response: Bus::new(10),
|
||||
// redirect: Bus::new(10),
|
||||
// theme: Bus::new(10),
|
||||
// locale_changed: Bus::new(10),
|
||||
// token_refreshed: Bus::new(10),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_handshake(&mut self) -> BusReader<PayloadHanshake> {
|
||||
// self.handshake.add_rx()
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_response(&mut self) -> BusReader<PayloadResponse> {
|
||||
// self.response.add_rx()
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_redirect(&mut self) -> BusReader<PayloadRedirect> {
|
||||
// self.redirect.add_rx()
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_theme(&mut self) -> BusReader<PayloadTheme> {
|
||||
// self.theme.add_rx()
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_locale_changed(&mut self) -> BusReader<PayloadLocaleChanged> {
|
||||
// self.locale_changed.add_rx()
|
||||
// }
|
||||
//
|
||||
// pub fn subscribe_token_refreshed(&mut self) -> BusReader<PayloadTokenRefreshed> {
|
||||
// self.token_refreshed.add_rx()
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[derive(EnumIter, Debug)]
|
||||
// pub enum EventType {
|
||||
// Handshake,
|
||||
// Response,
|
||||
// Redirect,
|
||||
// Theme,
|
||||
// LocaleChanged,
|
||||
// TokenRefreshed,
|
||||
// }
|
||||
|
||||
/**
|
||||
Events are what the dashboard sends and app receives on `window`
|
||||
*/
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[serde(tag = "type", content = "payload")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
@ -103,7 +47,7 @@ pub enum NotificationStatus {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PayloadHanshake {
|
||||
pub token: String,
|
||||
pub version: f32,
|
||||
pub version: i32,
|
||||
pub saleor_version: Option<String>,
|
||||
pub dashboard_version: Option<String>,
|
||||
}
|
||||
|
@ -118,8 +62,7 @@ pub struct PayloadResponse {
|
|||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PayloadRedirect {
|
||||
pub to: String,
|
||||
pub new_context: Option<bool>,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||
|
|
|
@ -8,11 +8,12 @@ use serde::{Deserialize, Serialize};
|
|||
use strum_macros::{EnumString, IntoStaticStr};
|
||||
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
|
||||
|
||||
use crate::{locales::LocaleCode, manifest::AppPermission};
|
||||
use crate::manifest::{AppPermission, LocaleCode};
|
||||
|
||||
use self::event::Event;
|
||||
use web_sys::{console, js_sys::JSON};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct AppBridge {
|
||||
pub state: AppBridgeState,
|
||||
pub referer_origin: Option<String>,
|
||||
|
@ -24,7 +25,7 @@ pub struct AppBridge {
|
|||
auto_notify_ready: bool,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct AppBridgeState {
|
||||
pub token: Option<String>,
|
||||
pub id: String,
|
||||
|
@ -84,7 +85,7 @@ impl AppBridgeState {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct AppBridgeUser {
|
||||
/**
|
||||
* Original permissions of the user that is using the app.
|
||||
|
@ -108,7 +109,7 @@ pub enum AppIframeParams {
|
|||
Locale,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, EnumString)]
|
||||
#[derive(Debug, Serialize, Deserialize, EnumString, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ThemeType {
|
||||
Light,
|
||||
|
@ -142,7 +143,7 @@ impl AppBridge {
|
|||
console::log_1(&"Referrer origin is none".into());
|
||||
}
|
||||
|
||||
let mut bridge = Self {
|
||||
let bridge = Self {
|
||||
auto_notify_ready,
|
||||
state: match AppBridgeState::from_window() {
|
||||
Ok(s) => s,
|
||||
|
@ -151,62 +152,56 @@ impl AppBridge {
|
|||
referer_origin: referrer,
|
||||
};
|
||||
if bridge.auto_notify_ready {
|
||||
bridge.notify_ready()?;
|
||||
dispatch_event(Action::NotifyReady("".into()))?;
|
||||
}
|
||||
Ok(bridge)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* make sure to keep the returned closure handle safe, once it deallocs events will no longer
|
||||
* trigger
|
||||
*/
|
||||
pub fn listen_to_events(
|
||||
&mut self,
|
||||
mut on_event: impl FnMut(Result<Event, serde_wasm_bindgen::Error>) + 'static,
|
||||
) -> Result<Closure<dyn FnMut(JsValue)>, AppBridgeError> {
|
||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||
let cb = Closure::wrap(Box::new(move |e: JsValue| {
|
||||
web_sys::console::log_1(
|
||||
&format!(
|
||||
"sdk::bridge::listen_to_events: {:?}",
|
||||
&JSON::stringify(&web_sys::js_sys::Reflect::get(&e, &"data".into()).unwrap())
|
||||
.unwrap()
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
let event_data: Result<Event, _> = serde_wasm_bindgen::from_value(
|
||||
/**
|
||||
* make sure to keep the returned closure handle safe, once it deallocs events will no longer
|
||||
* trigger
|
||||
*/
|
||||
pub fn listen_to_events(
|
||||
mut on_event: impl FnMut(Result<Event, serde_wasm_bindgen::Error>) + 'static,
|
||||
) -> Result<Closure<dyn FnMut(JsValue)>, AppBridgeError> {
|
||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||
let cb = Closure::wrap(Box::new(move |e: JsValue| {
|
||||
web_sys::console::log_1(
|
||||
&format!(
|
||||
"sdk::bridge::listen_to_events: {:?}",
|
||||
&JSON::stringify(&web_sys::js_sys::Reflect::get(&e, &"data".into()).unwrap())
|
||||
.unwrap()
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
let event_data: Result<Event, _> = serde_wasm_bindgen::from_value(
|
||||
web_sys::js_sys::Reflect::get(&e, &"data".into())
|
||||
.expect("Closure should've received object with .data property, but didn't, saleor plz fix?"),
|
||||
);
|
||||
// web_sys::console::log_1(&format!("{:?}", &event_data).into());
|
||||
on_event(event_data);
|
||||
}) as Box<dyn FnMut(JsValue)>);
|
||||
// web_sys::console::log_1(&format!("{:?}", &event_data).into());
|
||||
on_event(event_data);
|
||||
}) as Box<dyn FnMut(JsValue)>);
|
||||
|
||||
window
|
||||
.add_event_listener_with_callback("message", &cb.as_ref().unchecked_ref())
|
||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||
Ok(cb)
|
||||
}
|
||||
window
|
||||
.add_event_listener_with_callback("message", &cb.as_ref().unchecked_ref())
|
||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||
Ok(cb)
|
||||
}
|
||||
|
||||
pub fn dispatch_event(&mut self, action: Action) -> Result<(), AppBridgeError> {
|
||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||
let parent = match window.parent() {
|
||||
Ok(p) => p.ok_or(AppBridgeError::WindowParentIsUndefined)?,
|
||||
Err(e) => return Err(AppBridgeError::JsValue(e)),
|
||||
};
|
||||
// let message = JsValue::from(&event);
|
||||
let message = serde_wasm_bindgen::to_value(&action)?;
|
||||
web_sys::console::log_1(&message);
|
||||
parent
|
||||
.post_message(&message, "*")
|
||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn notify_ready(&mut self) -> Result<&mut Self, AppBridgeError> {
|
||||
self.dispatch_event(Action::NotifyReady("{}".to_owned()))?;
|
||||
Ok(self)
|
||||
}
|
||||
pub fn dispatch_event(action: Action) -> Result<(), AppBridgeError> {
|
||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||
let parent = match window.parent() {
|
||||
Ok(p) => p.ok_or(AppBridgeError::WindowParentIsUndefined)?,
|
||||
Err(e) => return Err(AppBridgeError::JsValue(e)),
|
||||
};
|
||||
// let message = JsValue::from(&event);
|
||||
let message = serde_wasm_bindgen::to_value(&action)?;
|
||||
web_sys::console::log_1(&message);
|
||||
parent
|
||||
.post_message(&message, "*")
|
||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
|
|
Loading…
Reference in a new issue