mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Unified prefs writing
This commit is contained in:
1 parent
957525393b
commit
4e9472c14a
8 files changed
+274
-69
No files matched your search
+8
-15
@@ -31,6 +31,7 @@ import { AddFilesDialog, type AddTarget } from './components/AddFilesDialog'
|
||||
import { ConvertStemsDialog } from './components/ConvertStemsDialog'
|
||||
import { StemReportDialog } from './components/StemReportDialog'
|
||||
import { Icon } from './lib/icons'
|
||||
import { usePrefsWriter } from './lib/prefs'
|
||||
|
||||
function applyFilters(tracks: Track[], f: Filters): Track[] {
|
||||
const q = f.search.trim().toLowerCase()
|
||||
@@ -117,7 +118,7 @@ export default function App() {
|
||||
const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({})
|
||||
const prefsQuery = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs })
|
||||
const hydratedRef = useRef(false)
|
||||
const saveTimer = useRef<number | null>(null)
|
||||
const writePrefs = usePrefsWriter()
|
||||
|
||||
const resetColumns = useCallback(() => {
|
||||
setColumnVisibility(DEFAULT_COLUMN_VISIBILITY)
|
||||
@@ -148,22 +149,14 @@ export default function App() {
|
||||
}
|
||||
}, [prefsQuery.data])
|
||||
|
||||
// Persist layout changes (debounced), but not before hydration so we never
|
||||
// clobber saved prefs with the initial defaults.
|
||||
// Persist layout changes (debounced, via the shared prefs writer), but not
|
||||
// before hydration so we never clobber saved prefs with the initial defaults.
|
||||
useEffect(() => {
|
||||
if (!hydratedRef.current) return
|
||||
if (saveTimer.current) window.clearTimeout(saveTimer.current)
|
||||
saveTimer.current = window.setTimeout(() => {
|
||||
api
|
||||
.patchPrefs({
|
||||
columns: { visibility: columnVisibility, order: columnOrder, sizing: columnSizing },
|
||||
})
|
||||
.catch(() => {})
|
||||
}, 500)
|
||||
return () => {
|
||||
if (saveTimer.current) window.clearTimeout(saveTimer.current)
|
||||
}
|
||||
}, [columnVisibility, columnOrder, columnSizing])
|
||||
writePrefs({
|
||||
columns: { visibility: columnVisibility, order: columnOrder, sizing: columnSizing },
|
||||
})
|
||||
}, [columnVisibility, columnOrder, columnSizing, writePrefs])
|
||||
|
||||
const collection = useQuery({ queryKey: ['collection'], queryFn: api.collection })
|
||||
const loaded = collection.data?.loaded ?? false
|
||||
|
||||
@@ -33,6 +33,7 @@ import { HotcueBar } from './HotcueBar'
|
||||
import { LoopControls, LOOP_SIZES, type LoopMode } from './LoopControls'
|
||||
import { MainWaveform, MIN_SEC, MAX_SEC, DEFAULT_SEC, type StemLane } from './MainWaveform'
|
||||
import { OverviewWaveform } from './OverviewWaveform'
|
||||
import { usePrefsWriter } from '../lib/prefs'
|
||||
|
||||
/** Keys that mute stems 1-4 (Shift = solo), by `KeyboardEvent.code`. */
|
||||
const STEM_KEYS = ['KeyQ', 'KeyW', 'KeyE', 'KeyR']
|
||||
@@ -165,11 +166,11 @@ export function PrepStrip({
|
||||
|
||||
// Main-waveform zoom persists to userprefs.json so it survives track switches
|
||||
// (MainWaveform unmounts during each load) and app restarts. Hydrate once from
|
||||
// the shared ['prefs'] query, then persist changes debounced (mirrors the
|
||||
// column-layout prefs flow in App).
|
||||
// the shared ['prefs'] query, then persist changes through the shared,
|
||||
// debounced prefs writer (as App's column layout does).
|
||||
const prefsQuery = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs })
|
||||
const zoomHydratedRef = useRef(false)
|
||||
const zoomSaveTimer = useRef<number | null>(null)
|
||||
const writePrefs = usePrefsWriter()
|
||||
useEffect(() => {
|
||||
if (zoomHydratedRef.current || !prefsQuery.data) return
|
||||
zoomHydratedRef.current = true
|
||||
@@ -180,20 +181,13 @@ export function PrepStrip({
|
||||
}, [prefsQuery.data])
|
||||
useEffect(() => {
|
||||
if (!zoomHydratedRef.current) return // don't clobber saved prefs pre-hydration
|
||||
if (zoomSaveTimer.current) window.clearTimeout(zoomSaveTimer.current)
|
||||
zoomSaveTimer.current = window.setTimeout(() => {
|
||||
api.patchPrefs({ mainZoomSec: secPerView }).catch(() => {})
|
||||
}, 500)
|
||||
return () => {
|
||||
if (zoomSaveTimer.current) window.clearTimeout(zoomSaveTimer.current)
|
||||
}
|
||||
}, [secPerView])
|
||||
writePrefs({ mainZoomSec: secPerView })
|
||||
}, [secPerView, writePrefs])
|
||||
|
||||
// The loop/jump size persists the same way (survives track switches +
|
||||
// restarts). `beatJumpBeats` is its name from when only beat jump had a size;
|
||||
// it is read once so an upgrade keeps the user's choice.
|
||||
const sizeHydratedRef = useRef(false)
|
||||
const sizeSaveTimer = useRef<number | null>(null)
|
||||
useEffect(() => {
|
||||
if (sizeHydratedRef.current || !prefsQuery.data) return
|
||||
sizeHydratedRef.current = true
|
||||
@@ -203,14 +197,8 @@ export function PrepStrip({
|
||||
}, [prefsQuery.data])
|
||||
useEffect(() => {
|
||||
if (!sizeHydratedRef.current) return // don't clobber saved prefs pre-hydration
|
||||
if (sizeSaveTimer.current) window.clearTimeout(sizeSaveTimer.current)
|
||||
sizeSaveTimer.current = window.setTimeout(() => {
|
||||
api.patchPrefs({ beatSize }).catch(() => {})
|
||||
}, 500)
|
||||
return () => {
|
||||
if (sizeSaveTimer.current) window.clearTimeout(sizeSaveTimer.current)
|
||||
}
|
||||
}, [beatSize])
|
||||
writePrefs({ beatSize })
|
||||
}, [beatSize, writePrefs])
|
||||
|
||||
const getCtx = (): AudioContext => {
|
||||
if (!audioCtxRef.current) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createPortal } from 'react-dom'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { flushPrefs } from '../lib/prefs'
|
||||
|
||||
/**
|
||||
* Ask before quitting with unsaved changes: Save · Discard · Cancel.
|
||||
@@ -46,6 +47,7 @@ export function QuitGuard() {
|
||||
.then((s) => s.dirty)
|
||||
.catch(() => false) // no library loaded: nothing to lose
|
||||
if (!unsaved) {
|
||||
await flushPrefs(1000)
|
||||
await invoke('quit_now')
|
||||
return
|
||||
}
|
||||
@@ -73,6 +75,7 @@ export function QuitGuard() {
|
||||
}, [dirty])
|
||||
|
||||
const quit = async () => {
|
||||
await flushPrefs(1000) // a layout change made just before quitting still lands
|
||||
const { invoke } = await import('@tauri-apps/api/core')
|
||||
await invoke('quit_now')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { useCallback } from 'react'
|
||||
import { useQueryClient, type QueryClient } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
|
||||
/**
|
||||
* The one debounced writer for prefs that change continuously (column layout,
|
||||
* deck zoom, loop size).
|
||||
*
|
||||
* Each of those used to own a 500 ms timer. All of them hydrate from the same
|
||||
* ['prefs'] fetch on launch, so their timers fired together and sent several
|
||||
* PATCHes at once, which is the burst that once wiped userprefs.json (the
|
||||
* backend now serialises them too). Here every change is merged into one
|
||||
* pending patch, sent as ONE request after a quiet 500 ms, and requests go out
|
||||
* strictly in order, so an older response can never overwrite a newer cache.
|
||||
*
|
||||
* The queue is module-level, so a component unmounting does not cancel its
|
||||
* pending write; `flushPrefs()` sends it at once (QuitGuard calls it before
|
||||
* quitting).
|
||||
*
|
||||
* One-off writes from a dialog may still call `api.patchPrefs` directly.
|
||||
*/
|
||||
const DEBOUNCE_MS = 500
|
||||
|
||||
let pending: Record<string, unknown> = {}
|
||||
let timer: number | null = null
|
||||
let client: QueryClient | null = null
|
||||
let chain: Promise<void> = Promise.resolve()
|
||||
|
||||
export function queuePrefs(qc: QueryClient, patch: Record<string, unknown>): void {
|
||||
client = qc
|
||||
Object.assign(pending, patch)
|
||||
if (timer !== null) window.clearTimeout(timer)
|
||||
timer = window.setTimeout(() => void flushPrefs(), DEBOUNCE_MS)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send anything queued now. Resolves once every write so far has landed, or
|
||||
* after `timeoutMs` if given — quitting must not wait on a hung backend.
|
||||
*/
|
||||
export function flushPrefs(timeoutMs?: number): Promise<void> {
|
||||
if (timer !== null) {
|
||||
window.clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
if (Object.keys(pending).length) {
|
||||
const patch = pending
|
||||
pending = {}
|
||||
chain = chain.then(() =>
|
||||
api
|
||||
.patchPrefs(patch)
|
||||
.then((p) => {
|
||||
client?.setQueryData(['prefs'], p)
|
||||
})
|
||||
.catch(() => {}),
|
||||
)
|
||||
}
|
||||
if (timeoutMs === undefined) return chain
|
||||
return Promise.race([chain, new Promise<void>((r) => window.setTimeout(r, timeoutMs))])
|
||||
}
|
||||
|
||||
/** `queuePrefs` bound to this tree's QueryClient. */
|
||||
export function usePrefsWriter(): (patch: Record<string, unknown>) => void {
|
||||
const qc = useQueryClient()
|
||||
return useCallback((patch) => queuePrefs(qc, patch), [qc])
|
||||
}
|
||||
Reference in new issue
Block a user