get bridge working!

This commit is contained in:
djkato 2024-11-15 01:38:19 +01:00
parent 242fd2510f
commit 178f3190ce
3 changed files with 58 additions and 116 deletions

View file

@ -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)]

View file

@ -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)]

View file

@ -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,17 +152,17 @@ 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)?;
@ -188,7 +189,7 @@ impl AppBridge {
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 parent = match window.parent() {
Ok(p) => p.ok_or(AppBridgeError::WindowParentIsUndefined)?,
@ -203,12 +204,6 @@ impl AppBridge {
Ok(())
}
pub fn notify_ready(&mut self) -> Result<&mut Self, AppBridgeError> {
self.dispatch_event(Action::NotifyReady("{}".to_owned()))?;
Ok(self)
}
}
#[derive(thiserror::Error, Debug)]
pub enum AppBridgeError {
#[error("failed serializing event from window")]