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

10
package-lock.json generated
View File

@@ -27,7 +27,7 @@
"@tauri-apps/plugin-log": "^2.0.0-rc.1",
"@tauri-apps/plugin-os": "^2.0.0-rc.1",
"@tauri-apps/plugin-shell": "^2.0.0-rc.1",
"@yaakapp/api": "^0.1.17",
"@yaakapp/api": "^0.2.0",
"buffer": "^6.0.3",
"classnames": "^2.5.1",
"cm6-graphql": "^0.0.9",
@@ -3322,11 +3322,11 @@
"license": "MIT"
},
"node_modules/@yaakapp/api": {
"version": "0.1.17",
"resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.1.17.tgz",
"integrity": "sha512-VE9A0FDZwczZkTAbMOYjQOKzbW1KmaItq/mPSuTgU87Lf570JUepcHVtL7QFLV1U/R5q+n7I6xQg9Q2mDj/OWQ==",
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.2.0.tgz",
"integrity": "sha512-DskPYRQ0Hk3KcOIi8O5drbpK0wUwvpUcMvsYfPPz90jfwb9tSpprfFKFUiCVartrA/VO6R0skKscWz74QTKaSA==",
"dependencies": {
"@types/node": "^22.0.0"
"@types/node": "^22.5.4"
}
},
"node_modules/abbrev": {

View File

@@ -46,7 +46,7 @@
"@tauri-apps/plugin-log": "^2.0.0-rc.1",
"@tauri-apps/plugin-os": "^2.0.0-rc.1",
"@tauri-apps/plugin-shell": "^2.0.0-rc.1",
"@yaakapp/api": "^0.1.17",
"@yaakapp/api": "^0.2.0",
"buffer": "^6.0.3",
"classnames": "^2.5.1",
"cm6-graphql": "^0.0.9",

View File

@@ -1,6 +1,6 @@
{
"name": "@yaakapp/api",
"version": "0.1.17",
"version": "0.2.0",
"main": "lib/index.js",
"typings": "./lib/index.d.ts",
"files": [

View File

@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type EmptyResponse = {};
export type BootRequest = { dir: string, };

View File

@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type PluginBootRequest = { dir: string, };
export type BootResponse = { name: string, version: string, capabilities: Array<string>, };

View File

@@ -1,30 +1,55 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { CallHttpRequestActionRequest } from "./CallHttpRequestActionRequest";
import type { CallTemplateFunctionRequest } from "./CallTemplateFunctionRequest";
import type { CallTemplateFunctionResponse } from "./CallTemplateFunctionResponse";
import type { CopyTextRequest } from "./CopyTextRequest";
import type { EmptyResponse } from "./EmptyResponse";
import type { ExportHttpRequestRequest } from "./ExportHttpRequestRequest";
import type { ExportHttpRequestResponse } from "./ExportHttpRequestResponse";
import type { FilterRequest } from "./FilterRequest";
import type { FilterResponse } from "./FilterResponse";
import type { FindHttpResponsesRequest } from "./FindHttpResponsesRequest";
import type { FindHttpResponsesResponse } from "./FindHttpResponsesResponse";
import type { GetHttpRequestActionsRequest } from "./GetHttpRequestActionsRequest";
import type { GetHttpRequestActionsResponse } from "./GetHttpRequestActionsResponse";
import type { GetHttpRequestByIdRequest } from "./GetHttpRequestByIdRequest";
import type { GetHttpRequestByIdResponse } from "./GetHttpRequestByIdResponse";
import type { GetTemplateFunctionsResponse } from "./GetTemplateFunctionsResponse";
import type { ImportRequest } from "./ImportRequest";
import type { ImportResponse } from "./ImportResponse";
import type { PluginBootRequest } from "./PluginBootRequest";
import type { PluginBootResponse } from "./PluginBootResponse";
import type { PluginReloadRequest } from "./PluginReloadRequest";
import type { PluginReloadResponse } from "./PluginReloadResponse";
import type { RenderHttpRequestRequest } from "./RenderHttpRequestRequest";
import type { RenderHttpRequestResponse } from "./RenderHttpRequestResponse";
import type { SendHttpRequestRequest } from "./SendHttpRequestRequest";
import type { SendHttpRequestResponse } from "./SendHttpRequestResponse";
import type { ShowToastRequest } from "./ShowToastRequest";
import type { BootRequest } from './BootRequest';
import type { BootResponse } from './BootResponse';
import type { CallHttpRequestActionRequest } from './CallHttpRequestActionRequest';
import type { CallTemplateFunctionRequest } from './CallTemplateFunctionRequest';
import type { CallTemplateFunctionResponse } from './CallTemplateFunctionResponse';
import type { CopyTextRequest } from './CopyTextRequest';
import type { ExportHttpRequestRequest } from './ExportHttpRequestRequest';
import type { ExportHttpRequestResponse } from './ExportHttpRequestResponse';
import type { FilterRequest } from './FilterRequest';
import type { FilterResponse } from './FilterResponse';
import type { FindHttpResponsesRequest } from './FindHttpResponsesRequest';
import type { FindHttpResponsesResponse } from './FindHttpResponsesResponse';
import type { GetHttpRequestActionsRequest } from './GetHttpRequestActionsRequest';
import type { GetHttpRequestActionsResponse } from './GetHttpRequestActionsResponse';
import type { GetHttpRequestByIdRequest } from './GetHttpRequestByIdRequest';
import type { GetHttpRequestByIdResponse } from './GetHttpRequestByIdResponse';
import type { GetTemplateFunctionsResponse } from './GetTemplateFunctionsResponse';
import type { ImportRequest } from './ImportRequest';
import type { ImportResponse } from './ImportResponse';
import type { RenderHttpRequestRequest } from './RenderHttpRequestRequest';
import type { RenderHttpRequestResponse } from './RenderHttpRequestResponse';
import type { SendHttpRequestRequest } from './SendHttpRequestRequest';
import type { SendHttpRequestResponse } from './SendHttpRequestResponse';
import type { ShowToastRequest } from './ShowToastRequest';
export type InternalEventPayload = { "type": "boot_request" } & PluginBootRequest | { "type": "boot_response" } & PluginBootResponse | { "type": "reload_request" } & PluginReloadRequest | { "type": "reload_response" } & PluginReloadResponse | { "type": "import_request" } & ImportRequest | { "type": "import_response" } & ImportResponse | { "type": "filter_request" } & FilterRequest | { "type": "filter_response" } & FilterResponse | { "type": "export_http_request_request" } & ExportHttpRequestRequest | { "type": "export_http_request_response" } & ExportHttpRequestResponse | { "type": "send_http_request_request" } & SendHttpRequestRequest | { "type": "send_http_request_response" } & SendHttpRequestResponse | { "type": "get_http_request_actions_request" } & GetHttpRequestActionsRequest | { "type": "get_http_request_actions_response" } & GetHttpRequestActionsResponse | { "type": "call_http_request_action_request" } & CallHttpRequestActionRequest | { "type": "get_template_functions_request" } | { "type": "get_template_functions_response" } & GetTemplateFunctionsResponse | { "type": "call_template_function_request" } & CallTemplateFunctionRequest | { "type": "call_template_function_response" } & CallTemplateFunctionResponse | { "type": "copy_text_request" } & CopyTextRequest | { "type": "render_http_request_request" } & RenderHttpRequestRequest | { "type": "render_http_request_response" } & RenderHttpRequestResponse | { "type": "show_toast_request" } & ShowToastRequest | { "type": "get_http_request_by_id_request" } & GetHttpRequestByIdRequest | { "type": "get_http_request_by_id_response" } & GetHttpRequestByIdResponse | { "type": "find_http_responses_request" } & FindHttpResponsesRequest | { "type": "find_http_responses_response" } & FindHttpResponsesResponse | { "type": "empty_response" } & EmptyResponse;
export type InternalEventPayload =
| ({ type: 'boot_request' } & BootRequest)
| ({ type: 'boot_response' } & BootResponse)
| { type: 'reload_request' }
| { type: 'reload_response' }
| ({ type: 'import_request' } & ImportRequest)
| ({ type: 'import_response' } & ImportResponse)
| ({ type: 'filter_request' } & FilterRequest)
| ({ type: 'filter_response' } & FilterResponse)
| ({ type: 'export_http_request_request' } & ExportHttpRequestRequest)
| ({ type: 'export_http_request_response' } & ExportHttpRequestResponse)
| ({ type: 'send_http_request_request' } & SendHttpRequestRequest)
| ({ type: 'send_http_request_response' } & SendHttpRequestResponse)
| ({ type: 'get_http_request_actions_request' } & GetHttpRequestActionsRequest)
| ({ type: 'get_http_request_actions_response' } & GetHttpRequestActionsResponse)
| ({ type: 'call_http_request_action_request' } & CallHttpRequestActionRequest)
| { type: 'get_template_functions_request' }
| ({ type: 'get_template_functions_response' } & GetTemplateFunctionsResponse)
| ({ type: 'call_template_function_request' } & CallTemplateFunctionRequest)
| ({ type: 'call_template_function_response' } & CallTemplateFunctionResponse)
| ({ type: 'copy_text_request' } & CopyTextRequest)
| ({ type: 'render_http_request_request' } & RenderHttpRequestRequest)
| ({ type: 'render_http_request_response' } & RenderHttpRequestResponse)
| ({ type: 'show_toast_request' } & ShowToastRequest)
| ({ type: 'get_http_request_by_id_request' } & GetHttpRequestByIdRequest)
| ({ type: 'get_http_request_by_id_response' } & GetHttpRequestByIdResponse)
| ({ type: 'find_http_responses_request' } & FindHttpResponsesRequest)
| ({ type: 'find_http_responses_response' } & FindHttpResponsesResponse)
| { type: 'empty_response' };

View File

@@ -1,3 +0,0 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type PluginBootResponse = { name: string, version: string, capabilities: Array<string>, };

View File

@@ -1,3 +0,0 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type PluginReloadRequest = {};

View File

@@ -1,3 +0,0 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type PluginReloadResponse = {};

View File

@@ -12,7 +12,6 @@ export * from './gen/CookieDomain';
export * from './gen/CookieExpires';
export * from './gen/CookieJar';
export * from './gen/CopyTextRequest';
export * from './gen/EmptyResponse';
export * from './gen/Environment';
export * from './gen/EnvironmentVariable';
export * from './gen/ExportHttpRequestRequest';
@@ -43,10 +42,8 @@ export * from './gen/InternalEvent';
export * from './gen/InternalEventPayload';
export * from './gen/KeyValue';
export * from './gen/Model';
export * from './gen/PluginBootRequest';
export * from './gen/PluginBootResponse';
export * from './gen/PluginReloadRequest';
export * from './gen/PluginReloadResponse';
export * from './gen/BootRequest';
export * from './gen/BootResponse';
export * from './gen/RenderHttpRequestRequest';
export * from './gen/RenderHttpRequestResponse';
export * from './gen/RenderPurpose';

View File

@@ -313,8 +313,6 @@ function watchFile(filepath: string, cb: (filepath: string) => void) {
const stat = statSync(filepath);
if (stat.mtimeMs !== watchedFiles[filepath]?.mtimeMs) {
cb(filepath);
} else {
console.log('SKIPPING SAME FILE STAT', filepath, stat);
}
watchedFiles[filepath] = stat;
});

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,

View File

@@ -5,7 +5,6 @@ import { invokeCmd } from '../lib/tauri';
export function useTemplateFunctions() {
const result = useQuery({
queryKey: ['template_functions'],
refetchOnWindowFocus: false,
queryFn: async () => {
const responses = (await invokeCmd(
'cmd_template_functions',