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;
|
use crate::manifest::AppPermission;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Actions are what the dashboard receives and app sends on `window.parent`
|
||||||
|
*/
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
#[serde(tag = "type", content = "payload")]
|
#[serde(tag = "type", content = "payload")]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
@ -16,7 +19,8 @@ pub enum Action {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PayloadRequestPermissions {
|
pub struct PayloadRequestPermissions {
|
||||||
pub permissions: Vec<AppPermission>,
|
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)]
|
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||||
|
|
|
@ -1,67 +1,11 @@
|
||||||
use crate::{locales::LocaleCode, manifest::AppPermission};
|
use crate::manifest::{AppPermission, LocaleCode};
|
||||||
|
|
||||||
use super::ThemeType;
|
use super::ThemeType;
|
||||||
// use bus::{Bus, BusReader};
|
|
||||||
use serde::{Deserialize, Serialize};
|
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)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
#[serde(tag = "type", content = "payload")]
|
#[serde(tag = "type", content = "payload")]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
@ -103,7 +47,7 @@ pub enum NotificationStatus {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PayloadHanshake {
|
pub struct PayloadHanshake {
|
||||||
pub token: String,
|
pub token: String,
|
||||||
pub version: f32,
|
pub version: i32,
|
||||||
pub saleor_version: Option<String>,
|
pub saleor_version: Option<String>,
|
||||||
pub dashboard_version: Option<String>,
|
pub dashboard_version: Option<String>,
|
||||||
}
|
}
|
||||||
|
@ -118,8 +62,7 @@ pub struct PayloadResponse {
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PayloadRedirect {
|
pub struct PayloadRedirect {
|
||||||
pub to: String,
|
pub path: String,
|
||||||
pub new_context: Option<bool>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||||
|
|
|
@ -8,11 +8,12 @@ use serde::{Deserialize, Serialize};
|
||||||
use strum_macros::{EnumString, IntoStaticStr};
|
use strum_macros::{EnumString, IntoStaticStr};
|
||||||
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
|
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
|
||||||
|
|
||||||
use crate::{locales::LocaleCode, manifest::AppPermission};
|
use crate::manifest::{AppPermission, LocaleCode};
|
||||||
|
|
||||||
use self::event::Event;
|
use self::event::Event;
|
||||||
use web_sys::{console, js_sys::JSON};
|
use web_sys::{console, js_sys::JSON};
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct AppBridge {
|
pub struct AppBridge {
|
||||||
pub state: AppBridgeState,
|
pub state: AppBridgeState,
|
||||||
pub referer_origin: Option<String>,
|
pub referer_origin: Option<String>,
|
||||||
|
@ -24,7 +25,7 @@ pub struct AppBridge {
|
||||||
auto_notify_ready: bool,
|
auto_notify_ready: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct AppBridgeState {
|
pub struct AppBridgeState {
|
||||||
pub token: Option<String>,
|
pub token: Option<String>,
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
@ -84,7 +85,7 @@ impl AppBridgeState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct AppBridgeUser {
|
pub struct AppBridgeUser {
|
||||||
/**
|
/**
|
||||||
* Original permissions of the user that is using the app.
|
* Original permissions of the user that is using the app.
|
||||||
|
@ -108,7 +109,7 @@ pub enum AppIframeParams {
|
||||||
Locale,
|
Locale,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, EnumString)]
|
#[derive(Debug, Serialize, Deserialize, EnumString, Clone)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
pub enum ThemeType {
|
pub enum ThemeType {
|
||||||
Light,
|
Light,
|
||||||
|
@ -142,7 +143,7 @@ impl AppBridge {
|
||||||
console::log_1(&"Referrer origin is none".into());
|
console::log_1(&"Referrer origin is none".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut bridge = Self {
|
let bridge = Self {
|
||||||
auto_notify_ready,
|
auto_notify_ready,
|
||||||
state: match AppBridgeState::from_window() {
|
state: match AppBridgeState::from_window() {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
|
@ -151,19 +152,19 @@ impl AppBridge {
|
||||||
referer_origin: referrer,
|
referer_origin: referrer,
|
||||||
};
|
};
|
||||||
if bridge.auto_notify_ready {
|
if bridge.auto_notify_ready {
|
||||||
bridge.notify_ready()?;
|
dispatch_event(Action::NotifyReady("".into()))?;
|
||||||
}
|
}
|
||||||
Ok(bridge)
|
Ok(bridge)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* make sure to keep the returned closure handle safe, once it deallocs events will no longer
|
* make sure to keep the returned closure handle safe, once it deallocs events will no longer
|
||||||
* trigger
|
* trigger
|
||||||
*/
|
*/
|
||||||
pub fn listen_to_events(
|
pub fn listen_to_events(
|
||||||
&mut self,
|
|
||||||
mut on_event: impl FnMut(Result<Event, serde_wasm_bindgen::Error>) + 'static,
|
mut on_event: impl FnMut(Result<Event, serde_wasm_bindgen::Error>) + 'static,
|
||||||
) -> Result<Closure<dyn FnMut(JsValue)>, AppBridgeError> {
|
) -> Result<Closure<dyn FnMut(JsValue)>, AppBridgeError> {
|
||||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||||
let cb = Closure::wrap(Box::new(move |e: JsValue| {
|
let cb = Closure::wrap(Box::new(move |e: JsValue| {
|
||||||
web_sys::console::log_1(
|
web_sys::console::log_1(
|
||||||
|
@ -186,9 +187,9 @@ impl AppBridge {
|
||||||
.add_event_listener_with_callback("message", &cb.as_ref().unchecked_ref())
|
.add_event_listener_with_callback("message", &cb.as_ref().unchecked_ref())
|
||||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||||
Ok(cb)
|
Ok(cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_event(&mut self, action: Action) -> Result<(), AppBridgeError> {
|
pub fn dispatch_event(action: Action) -> Result<(), AppBridgeError> {
|
||||||
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
let window = web_sys::window().ok_or(AppBridgeError::WindowIsUndefined)?;
|
||||||
let parent = match window.parent() {
|
let parent = match window.parent() {
|
||||||
Ok(p) => p.ok_or(AppBridgeError::WindowParentIsUndefined)?,
|
Ok(p) => p.ok_or(AppBridgeError::WindowParentIsUndefined)?,
|
||||||
|
@ -201,12 +202,6 @@ impl AppBridge {
|
||||||
.post_message(&message, "*")
|
.post_message(&message, "*")
|
||||||
.map_err(|e| AppBridgeError::JsValue(e))?;
|
.map_err(|e| AppBridgeError::JsValue(e))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
|
||||||
|
|
||||||
pub fn notify_ready(&mut self) -> Result<&mut Self, AppBridgeError> {
|
|
||||||
self.dispatch_event(Action::NotifyReady("{}".to_owned()))?;
|
|
||||||
Ok(self)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
|
Loading…
Reference in a new issue