兼容enhance profile

This commit is contained in:
ctaoist 2022-05-20 16:18:02 +08:00
parent f115e4774a
commit e628e21d99
3 changed files with 27 additions and 12 deletions

View file

@ -93,7 +93,7 @@ impl Core {
if silent_start.unwrap_or(false) {
let window = self.window.lock();
window.as_ref().map(|win| {
win.close().unwrap();
win.hide().unwrap();
});
}
@ -382,7 +382,13 @@ impl Core {
result.error.map(|err| log::error!("{err}"));
});
window.emit("script-handler", payload).unwrap();
let verge = self.verge.lock();
let silent_start = verge.enable_silent_start.clone();
if silent_start.unwrap_or(false) {
window.emit("script-handler-close", payload).unwrap();
} else {
window.emit("script-handler", payload).unwrap();
}
Ok(())
}

View file

@ -182,7 +182,7 @@ fn main() -> std::io::Result<()> {
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|app_handle, e| match e {
tauri::RunEvent::ExitRequested { .. } => {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
tauri::RunEvent::Exit => {

View file

@ -1,4 +1,5 @@
import { emit, listen } from "@tauri-apps/api/event";
import { emit, listen, Event } from "@tauri-apps/api/event";
import { appWindow } from "@tauri-apps/api/window";
import { CmdType } from "./types";
import ignoreCase from "../utils/ignore-case";
@ -124,21 +125,29 @@ class Enhance {
return this.resultMap.get(uid);
}
async enhanceHandler(event: Event<unknown>) {
const payload = event.payload as CmdType.EnhancedPayload;
const result = await this.runner(payload).catch((err: any) => ({
data: null,
status: "error",
error: err.message,
}));
emit(payload.callback, JSON.stringify(result)).catch(console.error);
}
// setup the handler
setup() {
if (this.isSetup) return;
this.isSetup = true;
listen("script-handler", async (event) => {
const payload = event.payload as CmdType.EnhancedPayload;
await this.enhanceHandler(event);
});
const result = await this.runner(payload).catch((err: any) => ({
data: null,
status: "error",
error: err.message,
}));
emit(payload.callback, JSON.stringify(result)).catch(console.error);
listen("script-handler-close", async (event) => {
await this.enhanceHandler(event);
appWindow.close();
});
}