Reload template functions on window focus

This commit is contained in:
Gregory Schier
2024-09-17 09:48:53 -07:00
parent 024edb6674
commit e0c00579af
18 changed files with 87 additions and 81 deletions

View File

@@ -10,6 +10,7 @@ use std::time::Duration;
use crate::render::render_http_request;
use crate::response_err;
use crate::template_callback::PluginTemplateCallback;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use http::header::{ACCEPT, USER_AGENT};
use http::{HeaderMap, HeaderName, HeaderValue};
@@ -204,7 +205,7 @@ pub async fn send_http_request<R: Runtime>(
.unwrap_or_default();
let auth = format!("{username}:{password}");
let encoded = base64::engine::general_purpose::STANDARD.encode(auth);
let encoded = BASE64_STANDARD.encode(auth);
headers.insert(
"Authorization",
HeaderValue::from_str(&format!("Basic {}", encoded)).unwrap(),

View File

@@ -59,7 +59,7 @@ use yaak_models::queries::{
use yaak_plugin_runtime::events::{
CallHttpRequestActionRequest, FilterResponse, FindHttpResponsesResponse,
GetHttpRequestActionsResponse, GetHttpRequestByIdResponse, GetTemplateFunctionsResponse,
InternalEvent, InternalEventPayload, PluginBootResponse, RenderHttpRequestResponse,
InternalEvent, InternalEventPayload, BootResponse, RenderHttpRequestResponse,
SendHttpRequestResponse, ShowToastRequest, ToastVariant,
};
use yaak_plugin_runtime::handle::PluginHandle;
@@ -1473,7 +1473,7 @@ async fn cmd_plugin_info(
id: &str,
w: WebviewWindow,
plugin_manager: State<'_, PluginManager>,
) -> Result<PluginBootResponse, String> {
) -> Result<BootResponse, String> {
let plugin = get_plugin(&w, id).await.map_err(|e| e.to_string())?;
plugin_manager
.get_plugin_info(plugin.directory.as_str())
@@ -2085,7 +2085,7 @@ async fn handle_plugin_event<R: Runtime>(
},
))
}
InternalEventPayload::ReloadResponse(_) => {
InternalEventPayload::ReloadResponse => {
let w = get_focused_window_no_lock(app_handle).expect("No focused window");
let plugins = list_plugins(&w).await.unwrap();
for plugin in plugins {

View File

@@ -21,11 +21,11 @@ pub struct InternalEvent {
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export)]
pub enum InternalEventPayload {
BootRequest(PluginBootRequest),
BootResponse(PluginBootResponse),
BootRequest(BootRequest),
BootResponse(BootResponse),
ReloadRequest(EmptyResponse),
ReloadResponse(EmptyResponse),
ReloadRequest,
ReloadResponse,
ImportRequest(ImportRequest),
ImportResponse(ImportResponse),
@@ -63,25 +63,20 @@ pub enum InternalEventPayload {
/// Returned when a plugin doesn't get run, just so the server
/// has something to listen for
EmptyResponse(EmptyResponse),
EmptyResponse,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, type = "{}")]
pub struct EmptyResponse {}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export)]
pub struct PluginBootRequest {
pub struct BootRequest {
pub dir: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export)]
pub struct PluginBootResponse {
pub struct BootResponse {
pub name: String,
pub version: String,
pub capabilities: Vec<String>,

View File

@@ -1,4 +1,4 @@
use crate::events::{EmptyResponse, InternalEvent, InternalEventPayload, PluginBootResponse};
use crate::events::{BootResponse, InternalEvent, InternalEventPayload};
use crate::server::plugin_runtime::EventStreamEvent;
use crate::util::gen_id;
use std::sync::Arc;
@@ -9,7 +9,7 @@ pub struct PluginHandle {
pub ref_id: String,
pub dir: String,
pub(crate) to_plugin_tx: Arc<Mutex<mpsc::Sender<tonic::Result<EventStreamEvent>>>>,
pub(crate) boot_resp: Arc<Mutex<Option<PluginBootResponse>>>,
pub(crate) boot_resp: Arc<Mutex<Option<BootResponse>>>,
}
impl PluginHandle {
@@ -20,7 +20,7 @@ impl PluginHandle {
}
}
pub async fn info(&self) -> Option<PluginBootResponse> {
pub async fn info(&self) -> Option<BootResponse> {
let resp = &*self.boot_resp.lock().await;
resp.clone()
}
@@ -39,7 +39,7 @@ impl PluginHandle {
}
pub async fn reload(&self) -> crate::error::Result<()> {
let event = self.build_event_to_send(&InternalEventPayload::ReloadRequest(EmptyResponse{}), None);
let event = self.build_event_to_send(&InternalEventPayload::ReloadRequest, None);
self.send(&event).await
}
@@ -59,7 +59,7 @@ impl PluginHandle {
Ok(())
}
pub async fn boot(&self, resp: &PluginBootResponse) {
pub async fn boot(&self, resp: &BootResponse) {
let mut boot_resp = self.boot_resp.lock().await;
*boot_resp = Some(resp.clone());
}

View File

@@ -1,6 +1,6 @@
use crate::error::Result;
use crate::events::{
PluginBootResponse, CallHttpRequestActionRequest, CallTemplateFunctionArgs,
BootResponse, CallHttpRequestActionRequest, CallTemplateFunctionArgs,
CallTemplateFunctionRequest, CallTemplateFunctionResponse, FilterRequest, FilterResponse,
GetHttpRequestActionsRequest, GetHttpRequestActionsResponse, GetTemplateFunctionsResponse,
ImportRequest, ImportResponse, InternalEvent, InternalEventPayload, RenderPurpose,
@@ -69,7 +69,7 @@ impl PluginManager {
.await
}
pub async fn get_plugin_info(&self, dir: &str) -> Option<PluginBootResponse> {
pub async fn get_plugin_info(&self, dir: &str) -> Option<BootResponse> {
self.server.plugin_by_dir(dir).await.ok()?.info().await
}
@@ -204,7 +204,7 @@ impl PluginManager {
match event.payload {
InternalEventPayload::FilterResponse(resp) => Ok(resp),
InternalEventPayload::EmptyResponse(_) => {
InternalEventPayload::EmptyResponse => {
Err(PluginErr("Filter returned empty".to_string()))
}
e => Err(PluginErr(format!("Export returned invalid event {:?}", e))),

View File

@@ -10,7 +10,7 @@ use tonic::{Request, Response, Status, Streaming};
use crate::error::Error::PluginNotFoundErr;
use crate::error::Result;
use crate::events::{InternalEvent, InternalEventPayload, PluginBootRequest, PluginBootResponse};
use crate::events::{InternalEvent, InternalEventPayload, BootRequest, BootResponse};
use crate::handle::PluginHandle;
use crate::server::plugin_runtime::plugin_runtime_server::PluginRuntime;
use crate::util::gen_id;
@@ -75,7 +75,7 @@ impl PluginRuntimeGrpcServer {
};
}
pub async fn boot_plugin(&self, id: &str, resp: &PluginBootResponse) {
pub async fn boot_plugin(&self, id: &str, resp: &BootResponse) {
match self.plugin_ref_to_plugin.lock().await.get(id) {
None => println!("Tried booting non-existing plugin {}", id),
Some(plugin) => plugin.clone().boot(resp).await,
@@ -266,7 +266,7 @@ impl PluginRuntimeGrpcServer {
plugin_ids.push(plugin.clone().ref_id);
let event = plugin.build_event_to_send(
&InternalEventPayload::BootRequest(PluginBootRequest {
&InternalEventPayload::BootRequest(BootRequest {
dir: dir.to_string(),
}),
None,