Skip to content

Commit c7fe071

Browse files
committed
fix: fix the event listener leak issue
1 parent 069d459 commit c7fe071

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src-tauri/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ pub fn run() {
204204

205205
tauri::Builder::default()
206206
.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
207-
let _ = app
208-
.get_webview_window("main")
209-
.expect("no main window")
210-
.set_focus();
207+
if let Some(window) = app.get_webview_window("main") {
208+
let _ = window.show();
209+
let _ = window.set_focus();
210+
}
211211
}))
212212
.plugin(tauri_plugin_process::init())
213213
.plugin(tauri_plugin_opener::init())

src/stores/app.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { TauriAPI } from '../api/tauri'
55

66
type ActionFn<T = any> = () => Promise<T>
77

8+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
9+
let mediaQueryListener: ((e: MediaQueryListEvent) => void) | null = null
10+
811
export const useAppStore = defineStore('app', () => {
912
const settings = ref<MergedSettings>({
1013
openlist: { port: 5244, data_dir: '', auto_launch: false, ssl_enabled: false, binary_path: undefined },
@@ -97,7 +100,7 @@ export const useAppStore = defineStore('app', () => {
97100

98101
// Settings
99102
const loadSettings = () => {
100-
tryCatch(async () => {
103+
return tryCatch(async () => {
101104
const res = await TauriAPI.settings.load()
102105
if (res) settings.value = res
103106
applyTheme(settings.value.app.theme || 'light')
@@ -457,22 +460,25 @@ export const useAppStore = defineStore('app', () => {
457460

458461
function applyTheme(theme: string) {
459462
const root = document.documentElement
460-
root.classList.remove('light', 'dark', 'auto')
461463

464+
root.classList.remove('light', 'dark', 'auto')
465+
if (mediaQueryListener) {
466+
mediaQuery.removeEventListener('change', mediaQueryListener)
467+
mediaQueryListener = null
468+
}
462469
if (theme === 'auto') {
463470
root.classList.add('auto')
464471
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
465472
root.classList.add(prefersDark ? 'dark' : 'light')
466473
root.setAttribute('data-theme', prefersDark ? 'dark' : 'light')
467-
468-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
469-
mediaQuery.addEventListener('change', e => {
474+
mediaQueryListener = (e: MediaQueryListEvent) => {
470475
if (settings.value.app.theme === 'auto') {
471476
root.classList.remove('light', 'dark')
472477
root.classList.add(e.matches ? 'dark' : 'light')
473478
root.setAttribute('data-theme', e.matches ? 'dark' : 'light')
474479
}
475-
})
480+
}
481+
mediaQuery.addEventListener('change', mediaQueryListener)
476482
} else {
477483
root.classList.add(theme)
478484
root.setAttribute('data-theme', theme)

0 commit comments

Comments
 (0)